0ms C++ solutions


  • 0
    C
    class Solution {
    public:
        vector<int> spiralOrder(vector<vector<int>>& matrix) {
            vector<int> res;
            int m = matrix.size();
            if (m==0) return res;
            int n = matrix[0].size();
            int len = min(m,n);
            int i,j,k,p,q;
            for (i=0; i<len/2; i++){
                for (j=i; j<n-i;j++)
                    res.push_back(matrix[i][j]);
                for (k=i+1; k<m-i; k++)
                    res.push_back(matrix[k][n-i-1]);
                for (p=n-i-2; p>=i; p--)
                    res.push_back(matrix[m-i-1][p]);
                for (q=m-i-2; q>i;q--)
                    res.push_back(matrix[q][i]);
            }
            if (len%2!=0){
                if(m>=n){
                    for(int s=i; s<m-i;s++)
                        res.push_back(matrix[s][i]);
                }
                else{
                    for(int s=i; s<n-i;s++)
                        res.push_back(matrix[i][s]);
                }
            }
            return res;
            
        }
    };
    

Log in to reply
 

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