leetcode_54. Spiral Matrix

//https://leetcode.com/problems/spiral-matrix/#/description

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example,
Given the following matrix:

[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]

You should return [1,2,3,6,9,8,7,4,5].

题意就是求螺旋矩阵,但是有坑点是如果不输入,你得返回一个空值,真尴尬~


class Solution {



public:
    vector spiralOrder(vector>& matrix) {
        if (matrix.empty())
            return vector();
        int size = matrix.size();
        int size2 = matrix[0].size();
        //cout<         //cout<         int f[105][105];
        int v[10005];
        int i=0,j=-1;
        int count = 0;
        memset(f,0,sizeof(f));
        vector ve;
        while(count         {
            while(j+1             {
                v[count++]=matrix[i][++j];
                f[i][j]=1;
            }
            while(i+1             {
                v[count++]=matrix[++i][j];
                f[i][j]=1;
            }
            while(j-1>=0&&f[i][j-1]==0)
            {
                v[count++]=matrix[i][--j];
                f[i][j]=1;
            }
            while(i-1>=0&&f[i-1][j]==0)
            {
                v[count++]=matrix[--i][j];
                f[i][j]=1;
            }
        }
        for(int i=0;i         {
            ve.push_back(v[i]);
        }
        return ve;
    }
};

你可能感兴趣的:(acm)