Run time error:" Last executed input: []] ". why this code has the NULL input issue?


  • 0
    H

    it is the code for Spiral Matrix.

    Class Solution {
    public:
        vector<int> spiralOrder(vector<vector<int> > &matrix) {
            int i,j;
            vector <int> result;
            int  direction = 0;
            int rs=0, cs = 0;
            int m =3, n= 3;
            int re = m-1;
            int ce = n-1;
            int cnt = 0;
            
            matrix.resize(3,  vector<int>(3,0));
            result.resize(3*3);
            
            for(i=0; i<m; i++)
                for(j=0; j< n; j++)
                    matrix[i][j] = i*n +j +1;
                    
            while (cnt <= m *n) {
                switch (direction%4) {
                case 0: // left-> right
                    for (i= rs, j=cs; j <= cs; j++) {
                        result[cnt++] = matrix[i][j];
                    };
                    rs++;
                    direction++;
                    break;
                case 1:// top -> down
                    for (i= rs, j=ce; i <= re; i++) {
                        result[cnt++] = matrix[i][j];
                      
                    };
                    ce--;
                    direction++;
                    break;
                case 2: // right -> left
                    for(i=re, j=ce; j >= cs; j--) {
                        result[cnt++] = matrix[i][j];
                    };
                    re--;
                    direction++;
                    break;
                case 3: // bottom -> up
                    for(i=re, j=cs; i >= rs; i--) {
                        result[cnt++] = matrix[i][j];
                    };
                    cs++;
                    direction++;
                    break;
                };
            };
            return result;
        }
    };

Log in to reply
 

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