C++ SpiralMatrix code but getting runtime error.


  • 0
    C

    I am getting runtime error with this code when I submit. I wish it could give more details then just say runtime. Can anyone see if there is anything wrong in this code?

    class Solution {
    public:
        vector<int> spiralOrder(vector<vector<int>>& matrix) {
            
            if (matrix.empty())
            return vector<int>();
            
            if (matrix[0].empty()) {
    			return vector<int>();
    		}
            
            int row = matrix.size();
            int col = matrix[0].size();
            int siz = row*col;
            
            int left=0, top=0, right=col-1, bottom=row-1, index = 0;
        
            
            vector<int> myArr(siz);
            
            cout << right << "  " << bottom;
            
            while(top <= bottom && left <= right) {
                for(int i = left; i<=right; i++) 
                    myArr.push_back(matrix[top][i]);
                top++;
                
                for(int i = top; i<=bottom; i++) 
                    myArr.push_back(matrix[i][right]);
                right--;
                
                for(int i = right; i<=left; i++) 
                    myArr.push_back(matrix[bottom][i]);
                bottom--;
                
                for(int i = bottom; i<=top; i++) 
                    myArr.push_back(matrix[i][left]);
                left++;
            }
            
            return myArr;
        }
    };
    

Log in to reply
 

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