Easy to understand c++ solution . 2ms


  • 0
    N
    class Solution {
    
    public:
        vector<int> spiralOrder(vector<vector<int> > &matrix) {
    		enum direction{up,right,left,down};
            vector<int> res;
            int M=matrix.size();
            if(M==0) return res;
            int N=matrix[0].size();
            
            int ubound=0;
            int bbound=matrix.size()-1;
            int lbound=0;
            int rbound=matrix[0].size()-1;
            int num=0;
            
            direction dir=right;
            while(num<M*N){
                switch(dir){
                    case right:
                        for(int i=lbound;i<=rbound;i++){
                            res.push_back(matrix[ubound][i]);
                            num++;
                        }
                        ubound++;
                        dir=down;
                        break;
                        
                    case down:
                        for(int i=ubound;i<=bbound;i++){
                            res.push_back(matrix[i][rbound]);
                            num++;
                        }
                        rbound--;
                        dir=left;
                        break;
                    case left:
                        for(int i=rbound;i>=lbound;i--){
                            res.push_back(matrix[bbound][i]);
                            num++;
                        }
                        bbound--;
                        dir=up;
                        break;
                    case up:
                        for(int i=bbound;i>=ubound;i--){
                            res.push_back(matrix[i][lbound]);
                            num++;
                        }
                        lbound++;
                        dir=right;
                        break;
                }
            }
            return res;
            
        }
    };

Log in to reply
 

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