【问题标题】:My code is right but not accepted by Leetcode platoform. (ZigZag Conversion)我的代码是正确的,但没有被 Leetcode 平台接受。 (之字形转换)
【发布时间】:2020-10-11 05:42:03
【问题描述】:

是string子类下的leet代码problem,中等问题。

查询:我的程序在运行时为所有测试用例返回正确的结果,但是当我提交时,相同的测试用例没有通过。

我还制作了一个视频,click here 观看。

我的代码是:

string convert(string s, int numRows) {
        
        int loc_rows = numRows-2;
        int i=0;
        int a=0,b=0;
        int arr[1000][1000];
        while(i<s.length())
        {
            if(a<numRows)
            {
                arr[a][b] = s[i];
                
                a++;
                i++;
            }
            else if(a>=numRows)
            {   
                if(loc_rows>=1)
                {
                    b++;
                    arr[loc_rows][b]=s[i];
                    i++;
                    loc_rows--;
                    
                }
                else{
                    loc_rows=numRows-2;
                    b++;
                    a=0;
                }
                
            }
        }
        
        string result="";
        for(int d=0;d<numRows;d++)
        {
            for(int y=0;y<b+1;y++)
            {
                char temp = (char)arr[d][y];
                if((temp>='a' and temp<='z') or (temp>='A' and temp<='Z')  )
                result+=temp;
            }
            
        }
        
        
        
        return result;
        
    }

【问题讨论】:

  • 只是备注:您的 arr 变量使用大约。堆栈上有 4 MB。可能太多了。
  • 要么分配给arr,要么分配给std::vector&lt;std::string&gt;
  • 请刷新the help pages,采取SO tour,阅读How to Ask,以及this question checklist。问题应该自包含,问题陈述和所有要求都应该在问题本身中,而不是作为链接。
  • @ChaudharySarimurrab 如果 code_fodder 发布的答案是正确的,那么如果您使用过std::vector,您将永远不会遇到问题,因为向量会自动将元素设置为 0。
  • 请记住,如果您使用arr 中的行作为字符串(c 字符串)进行输出,则除非您手动执行,否则它们不会nul-terminated。如果需要初始化数组,可以memset (arr, 0, 1000*1000*sizeof(int));

标签: c++ string c++14 std


【解决方案1】:

我认为问题可能是您未初始化的数组/变量。

尝试设置初始化您的数组:int arr[1000][1000] = {0};

您不能依赖这些数组中的数据,因此初始化这些值非常重要。

注意:这是因为您依赖数组中的空值不是字母 ([a-zA-Z])。这样您就可以使用最终循环重新构建输出,该循环仅尝试打印字符。这是第一次工作,因为幸运的是 arr 在您的值之间的间隙中包含 0(或者至少不是字母)。第二次它包含第一次的一些垃圾(真的 - 你不知道这会是什么,但实际上它可能只是你上次留下的值)。因此,即使您每次都将正确的值输入 arr - 您的最终循环会在 array 中找到一些旧的 non-alpha 值 - 因此您的程序不正确...

【讨论】:

    【解决方案2】:

    或者,我们也可以使用 unsigned int 来提高效率:

    // The following block might slightly improve the execution time;
    // Can be removed;
    static const auto __optimize__ = []() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(NULL);
        std::cout.tie(NULL);
        return 0;
    }();
    
    
    // Most of headers are already included;
    // Can be removed;
    #include <cstdint>
    #include <vector>
    #include <string>
    
    static const struct Solution {
        using ValueType = std::uint_fast16_t;
        static const std::string convert(
            const std::string s,
            const int num_rows
        ) {
            if (num_rows == 1) {
                return s;
            }
    
            std::vector<std::string> res(num_rows);
            ValueType row = 0;
            ValueType direction = -1;
    
            for (ValueType index = 0; index < std::size(s); ++index) {
                if (!(index % (num_rows - 1))) {
                    direction *= -1;
                }
    
                res[row].push_back(s[index]);
                row += direction;
            }
    
            std::string converted;
    
            for (const auto& str : res) {
                converted += str;
            }
    
            return converted;
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多