# 螺旋矩阵
给你一个 m
行 n
列的矩阵 matrix
,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。
示例 1:
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]
示例 2:
输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]
提示:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 10
-100 <= matrix[i][j] <= 100
以下错误的选项是?
## aop
### before
```c
#include
using namespace std;
```
### after
```c
```
## 答案
```c
class Solution
{
public:
vector printMatrix(vector> matrix)
{
vector res;
if (matrix.size() == 0)
return res;
int r1 = 0, r2 = matrix.size() - 1;
int c1 = 0, c2 = matrix[0].size() - 1;
int i = 0, j = 0;
int sum = matrix.size() * matrix[0].size();
while (res.size() < sum)
{
if (j == c1 && i == r1)
{
while (j <= c2)
res.push_back(matrix[i][j++]);
++r1;
--j;
++i;
}
else if (j == c2 && i == r1)
{
while (i <= r2)
res.push_back(matrix[i++][j]);
++c2;
--i;
--j;
}
else if (j == c2 && i == r2)
{
while (j >= c1)
res.push_back(matrix[i][j--]);
--r2;
++j;
--i;
}
else
{
while (i >= r1)
res.push_back(matrix[i--][j]);
++c1;
++i;
++j;
}
}
return res;
}
};
```
## 选项
### A
```c
class Solution
{
public:
vector spiralOrder(vector> &matrix)
{
vector res;
int hor_top = 0;
int hor_bottom = matrix.size() - 1;
int ver_left = 0;
int ver_right = matrix[0].size() - 1;
int direction = 0;
while (hor_top <= hor_bottom && ver_left <= ver_right)
{
switch (direction)
{
case 0:
for (int i = ver_left; i <= ver_right; i++)
{
res.push_back(matrix[hor_top][i]);
}
hor_top++;
break;
case 1:
for (int i = hor_top; i <= hor_bottom; i++)
{
res.push_back(matrix[i][ver_right]);
}
ver_right--;
break;
case 2:
for (int i = ver_right; i >= ver_left; i--)
{
res.push_back(matrix[hor_bottom][i]);
}
hor_bottom--;
break;
case 3:
for (int i = hor_bottom; i >= hor_top; i--)
{
res.push_back(matrix[i][ver_left]);
}
ver_left++;
break;
default:
break;
}
direction++;
direction %= 4;
}
return res;
}
};
```
### B
```c
class Solution
{
public:
vector spiralOrder(vector> &matrix)
{
vector res;
if (matrix.size() == 0)
return res;
int up = 0, down = matrix.size() - 1, left = 0, right = matrix[0].size() - 1;
while (up <= down && left <= right)
{
for (int i = left; i <= right; i++)
res.push_back(matrix[up][i]);
for (int i = up + 1; i <= down; i++)
res.push_back(matrix[i][right]);
if (up < down && left < right)
{
for (int i = right - 1; i > left; i--)
res.push_back(matrix[down][i]);
for (int i = down; i > up; i--)
res.push_back(matrix[i][left]);
}
up++;
down--;
left++;
right--;
}
return res;
}
};
```
### C
```c
class Solution
{
public:
vector spiralOrder(vector> &matrix)
{
vector path;
if (matrix.empty())
return path;
int rows = matrix.size(), cols = matrix[0].size();
int top = 0, bottom = rows - 1, left = 0, right = cols - 1;
while (true)
{
for (int col = left; col <= right; ++col)
path.push_back(matrix[top][col]);
++top;
if (top > bottom)
break;
for (int row = top; row <= bottom; ++row)
path.push_back(matrix[row][right]);
--right;
if (right < left)
break;
for (int col = right; col >= left; --col)
path.push_back(matrix[bottom][col]);
--bottom;
if (top > bottom)
break;
for (int row = bottom; row >= top; --row)
path.push_back(matrix[row][left]);
++left;
if (right < left)
break;
}
return path;
}
};
```