forked from cmhungsteve/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiral-matrix.cpp
89 lines (81 loc) · 2.48 KB
/
spiral-matrix.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
// Time: O(m * n)
// Space: O(1)
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res;
if (matrix.empty()) {
return res;
}
for (int left = 0, right = matrix[0].size() - 1,
top = 0, bottom = matrix.size() - 1;
left <= right && top <= bottom;
++left, --right, ++top, --bottom) {
for (int j = left; j <= right; ++j) {
res.emplace_back(matrix[top][j]);
}
for (int i = top + 1; i < bottom; ++i) {
res.emplace_back(matrix[i][right]);
}
for (int j = right; top < bottom && j >= left; --j) {
res.emplace_back(matrix[bottom][j]);
}
for (int i = bottom - 1; left < right && i > top; --i) {
res.emplace_back(matrix[i][left]);
}
}
return res;
}
};
// Time: O(m * n)
// Space: O(1)
class Solution2 {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
const int m = matrix.size();
vector<int> res;
if (m == 0) {
return res;
}
const int n = matrix.front().size();
enum Action {RIGHT, DOWN, LEFT, UP};
Action action = RIGHT;
for (int i = 0, j = 0, begini = 0, beginj = 0, endi = m,
endj = n, cnt = 0, total = m * n; cnt < total; ++cnt) {
res.emplace_back(matrix[i][j]);
switch (action) {
case RIGHT:
if (j + 1 < endj) {
++j;
} else {
action = DOWN, ++begini, ++i;
}
break;
case DOWN:
if (i + 1 < endi) {
++i;
} else {
action = LEFT, --endj, --j;
}
break;
case LEFT:
if (j - 1 >= beginj) {
--j;
} else {
action = UP, --endi, --i;
}
break;
case UP:
if (i - 1 >= begini) {
--i;
} else {
action = RIGHT, ++beginj, ++j;
}
break;
default:
break;
}
}
return res;
}
};