C++ solution, Run time Error, Why?


  • 0
    X

    Hi, everyone, could anybody help me to find why my solution has Run Time Error? I wrote a program to test my code, and it output the expected answer.

     class Solution {
        public:
            string convert(string s, int nRows) {
                if (nRows == 1){
                    return s;
                }
                int l = s.size();
                vector<vector<string> > zigzag(nRows, vector<string>(l/nRows + 1, ""));
                int x = 0;
                int y = 0;
                for (int i = 0; i < l; i++){
                    zigzag[x][y] = s.substr(i,1);
                    if ( i % (2*nRows - 2) < nRows - 1){
                        x++;
                    }else{
                        x--;
                        y++;
                    }
                }
                string res = "";
                for (int i = 0; i < nRows; i++){
                    for(int j = 0; j < l/nRows+1; j++){
                        res += zigzag[i][j];
                    }
                }
                return res;
            }
        };

Log in to reply
 

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