题解 | #螺旋矩阵#
螺旋矩阵
http://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> >& matrix)
{
vector<int>a;
int i=0, j=-1,flag=0;
int left = 0,right = matrix[0].size(),top = 0, bottom = matrix.size();
if (matrix.empty()) return a;
while (left < right && bottom > top)
{
while (j < right-1)
{
j++;
a.push_back(matrix[i][j]);
flag=1;
}
while (i < bottom-1 && flag>0)
{
i++;
a.push_back(matrix[i][j]);
flag=2;
}
while (j > left && flag>1)
{
j--;
a.push_back(matrix[i][j]);
flag=3;
}
while (i > top+1 && flag>2)
{
i--;
a.push_back(matrix[i][j]);
}
flag=0;
left++, right--, top++, bottom--;
}
return a;
}
};