vector<int> spiralOrder(vector<vector<int>>& matrix)
{
vector<int> vi;
if (matrix.size() == 0 || matrix[0].size() == 0)return vi;
int row = matrix[0].size(), line = matrix.size(), up = 0, left = 0, down = row - 1, right = line - 1;
while (1)
{
for (int i = left; i <= right; i++)vi.push_back(matrix[up][i]);
if (up++ > down)break;
for (int i = up; i <= down; i++)vi.push_back(matrix[i][right]);
if (right-- < left)break;
for (int i = right; i >= left; i--)vi.push_back(matrix[down][i]);
if (down-- < up)break;
for (int i = down; i >= up; i--)vi.push_back(matrix[left][i]);
if (left++ > right)break;
}
return vi;
}
54. 螺旋矩阵
最新推荐文章于 2024-02-13 14:48:31 发布