我们可以模拟按顺序移动的过程来输出每一个位置上的数字。为了方便移动我们可以定义一个二维数组directions用于记录每次移动之后当前的具体位置,并利用一个二维数组visited来记录哪些位置已经被访问过了。当下一步的位置超出当前的矩形范围或者已经被访问过时,我们需要改变前进的方向,使用 ( d i r e c t i o n I n d e x + 1 ) % 4 (directionIndex + 1) \% 4 (directionIndex+1)%4来控制具体应该加上哪一位。
class Solution {
public:
vector<vector<int>> directions{{0, 1},
{1, 0},
{0, -1},
{-1, 0}};
vector<int> spiralOrder(vector<vector<int>> &matrix) {
if (matrix.size() == 0) {
return {};
}
int rows = matrix.size(), columns = matrix[0].size();
vector<vector<bool>> visited(rows, vector<bool>(columns));
int total = rows * columns;
vector<int> order(total);
int row = 0, column = 0;
int directionIndex = 0;
for (int i = 0; i < total; i++) {
order[i] = matrix[row][column];
visited[row][column] = true;
int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {
directionIndex = (directionIndex + 1) % 4;
}
row += directions[directionIndex][0];
column += directions[directionIndex][1];
}
return order;
}
};
我们可以通过记录剩余矩形可访问区域的长宽坐标来进行后续的访问。我们可以假设当前可访问区域左上角的位置是
(
t
o
p
,
l
e
f
t
)
(top,left)
(top,left),右下角的位置是
(
b
o
t
t
o
m
,
r
i
g
h
t
)
(bottom,right)
(bottom,right)。我们可以按照这样的顺序进行依次访问:
1、从左到右遍历最上一行,从
(
t
o
p
,
l
e
f
t
)
(top,left)
(top,left)到
(
t
o
p
,
r
i
g
h
t
)
(top,right)
(top,right);
2、从上到下遍历最右列,从
(
t
o
p
+
1
,
r
i
g
h
t
)
(top+1,right)
(top+1,right)到
(
b
o
t
t
o
m
,
r
i
g
h
t
)
(bottom,right)
(bottom,right);
3、若
l
e
f
t
<
r
i
g
h
t
left
4、从下到上遍历最左一行,从
(
b
o
t
t
o
m
,
l
e
f
t
)
(bottom,left)
(bottom,left)到
(
t
o
p
+
1
,
l
e
f
t
)
(top+1,left)
(top+1,left)。
5、遍历完一层之后我们将
l
e
f
t
left
left和
t
o
p
top
top加一,将
b
o
t
t
o
m
bottom
bottom和
r
i
g
h
t
right
right减一。
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if (matrix.size() == 0 || matrix[0].size() == 0) {
return {};
}
int rows = matrix.size(), columns = matrix[0].size();
vector<int> order;
int left = 0, right = columns - 1, top = 0, bottom = rows - 1;
while (left <= right && top <= bottom) {
for (int column = left; column <= right; column++) {
order.push_back(matrix[top][column]);
}
for (int row = top + 1; row <= bottom; row++) {
order.push_back(matrix[row][right]);
}
if (left < right && top < bottom) {
for (int column = right - 1; column > left; column--) {
order.push_back(matrix[bottom][column]);
}
for (int row = bottom; row > top; row--) {
order.push_back(matrix[row][left]);
}
}
left++;
right--;
top++;
bottom--;
}
return order;
}
};