My c++ solution


  • 0
    S

    vector<int> spiralOrder(vector<vector<int>>& matrix) {

        vector<int> res;
    
        int i,j,c;
    
        if(matrix.empty())
    
    		return res;
    
        int row=matrix.size(),column=matrix[0].size();
    
        int circle=ceil(min(row,column)/2.0);
    
        for(int cir=0;cir<circle;cir++){
    
          int rLow=cir,rHigh=row-cir-1;
    
          int cLow=cir,cHigh=column-cir-1;
    
          for(j=cLow;j<=cHigh;j++)
    
            res.push_back(matrix[rLow][j]);
    
          for(j=rLow+1;j<=rHigh;j++)
    
            res.push_back(matrix[j][cHigh]);
    
          for(j=cHigh-1;j>=cLow&&rLow!=rHigh;j--) 
    
            res.push_back(matrix[rHigh][j]);
    
          for(j=rHigh-1;j>rLow&&cLow!=cHigh;j--)  
    
            res.push_back(matrix[j][cLow]);
    
        }
    
        return res;
    
    }

Log in to reply
 

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