C++ code gets Runtime Error after submission but was correct when running in "Custom Testcase" with same test case


  • 0
    W

    My C++ code gets Runtime Error after submission with the following test case:

    "clffrkeecbpdipufhidjcxjhrnxcxmjcxohqanxdrmgzebhnlmwpmhwdvthsfqueeexgrujigskmvrzgfwvrftwa"
    70

    However, if I copy the test to Custom Testcase and click "Run Code". The result is correct.

    Would you please help?

    class Solution {
    public:
    
        string convert(string s, int numRows) {
            
            char* input= new char[s.size()];
            char* output= new char[s.size()*2];
            strcpy(input,s.c_str());
    
            queue<char> *q = new queue<char>[numRows];
            
            int k=0;
            int column=1;
            while (input[k]!='\0')
            {
                if (column>=numRows)
                    column=1;
                    
                for (int i=0;i<numRows && input[k]!='\0';i++)
                {
                    if (column==1)
                    {
                        q[i].push(input[k]);
                        k++;
                    }
                    else
                    {
                        if (i==numRows-column)
                        {
                            q[i].push(input[k]);
                            k++;
                        }
                    }
                }
                column++;
            }
            
            k=0;
            for (int i=0;i<numRows;i++)
            {
                while(!q[i].empty())
                {
                    output[k]=q[i].front();
                    k++;
                    q[i].pop();
                }
            }
            output[k]='\0';        
        
            string so(output);
            
            return so;
        }
    };
    

    enter image description here


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.