class Solution {
public:
    vector<int> spiralOrder(vector<vector<int> > &matrix) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int m,n;
        int cir;
        int i,j;
        vector<int> re;
        if(matrix.size()==0)
            return re;
            
        m=matrix.size();
        n=matrix[0].size();
        
        cir=min(m,n);
        cir=(cir+1)/2;
        for(i=0;i<cir;i++)
        {
            for(j=i;j<i+n-(i)*2;j++)
                re.push_back(matrix[i][j]);
                
            for(j=i+1;j<i+1+m-(i+1)*2;j++)
                re.push_back(matrix[j][i+n-(i)*2-1]);
            
            if(i+1+m-(i+1)*2!=i)    
            for(j=i+n-(i)*2-1;j>=i;j--)
                re.push_back(matrix[i+1+m-(i+1)*2][j]);
             
            if(i+n-(i)*2-1!=i)   
            for(j=i+1+m-(i+1)*2-1;j>i;j--)
                re.push_back(matrix[j][i]);
        }
        return re;
        
    }
};

 

posted on 2013-09-21 17:26  宇睿  阅读(148)  评论(0编辑  收藏  举报