-
Notifications
You must be signed in to change notification settings - Fork 0
/
0054.螺旋矩阵.cpp
103 lines (98 loc) · 2.13 KB
/
0054.螺旋矩阵.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* @lc app=leetcode.cn id=54 lang=cpp
*
* [54] 螺旋矩阵
*
* https://leetcode-cn.com/problems/spiral-matrix/description/
*
* algorithms
* Medium (36.93%)
* Likes: 243
* Dislikes: 0
* Total Accepted: 31.4K
* Total Submissions: 83.4K
* Testcase Example: '[[1,2,3],[4,5,6],[7,8,9]]'
*
* 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
*
* 示例 1:
*
* 输入:
* [
* [ 1, 2, 3 ],
* [ 4, 5, 6 ],
* [ 7, 8, 9 ]
* ]
* 输出: [1,2,3,6,9,8,7,4,5]
*
*
* 示例 2:
*
* 输入:
* [
* [1, 2, 3, 4],
* [5, 6, 7, 8],
* [9,10,11,12]
* ]
* 输出: [1,2,3,4,8,12,11,10,9,5,6,7]
*
*
*/
// @lc code=start
#include <vector>
using namespace std;
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix)
{
if (matrix.size() == 0) {
return {};
}
int up, down, right, left;
up = 0;
down = matrix.size() - 1;
right = matrix[0].size() - 1;
left = 0;
int direction = 0;
int size = (down + 1) * (right + 1);
vector<int> res;
int n = 0;
int i = 0, j = -1;
while (n < size) {
direction = direction % 4;
if (direction == 0) {
++j;
if (j == right) {
++direction;
++up;
}
} else if (direction == 1) {
++i;
if (i == down) {
++direction;
--right;
}
} else if (direction == 2) {
--j;
if (j == left) {
++direction;
--down;
}
} else if (direction == 3) {
--i;
if (i == up) {
++direction;
++left;
}
}
res.push_back(matrix[i][j]);
++n;
}
return res;
}
};
/* 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16 */
// @lc code=end