-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path54.螺旋矩阵.cpp
63 lines (54 loc) · 1.76 KB
/
54.螺旋矩阵.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* @lc app=leetcode.cn id=54 lang=cpp
*
* [54] 螺旋矩阵
*/
// @lc code=start
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
int upper_bound = 0;
int lower_bound = m-1;
int left_bound = 0;
int right_bound = n-1;
vector<int> res; // 注意这里不能初始化,得利用 push_back 方法
while (res.size() < m * n) {
if (upper_bound <= lower_bound) {
// 在顶部从左向右遍历
for (int j = left_bound; j <= right_bound; j++) {
res.push_back(matrix[upper_bound][j]);
}
// 上边界下移
upper_bound++;
}
if (left_bound <= right_bound) {
// 在右侧从上向下遍历
for (int i = upper_bound; i <= lower_bound; i++) {
res.push_back(matrix[i][right_bound]);
}
// 右边界左移
right_bound--;
}
if (upper_bound <= lower_bound) {
// 在底部从右向左遍历
for (int j = right_bound; j >= left_bound; j--) {
res.push_back(matrix[lower_bound][j]);
}
// 下边界上移
lower_bound--;
}
if (left_bound <= right_bound) {
// 在左侧从下向上遍历
for (int i = lower_bound; i >= upper_bound; i--) {
res.push_back(matrix[i][left_bound]);
}
// 左边界右移
left_bound++;
}
}
return res;
}
};
// @lc code=end