My C++ solution


  • 0
    P
    class Solution {
    public:
        vector<int> spiralOrder(vector<vector<int>>& matrix) {
            vector<int> spi;
            if(matrix.size()==0) return spi;
            int i=0,j=0,k=0,nR=matrix.size(),nC=matrix[0].size();
            int N=nR*nC;
            spi.assign(N,0);
            while(k<N){
                spi[k] = matrix[i][j];
                k++;
                if(i<(nR+1)/2 && i<nC-j-1 && i<=j+1) j++;
                else if(j>=nC/2 && i>=nC-j-1 && nC-j-1<nR-i-1) i++;
                else if(i>=(nR+1)/2 && nR-i-1<=nC-j-1 && nR-i-1<j) j--;
                else i--;
            }
            return spi;
        }
    };

Log in to reply
 

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