-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiral-matrix.cc
67 lines (63 loc) · 1.7 KB
/
spiral-matrix.cc
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
#include "leetcode.h"
using namespace std;
class Solution {
public:
vector<int> spiralOrder(const vector<vector<int>>& matrix) {
vector<int> res;
if (matrix.empty()) {
return res;
}
res.reserve(matrix.size() * matrix[0].size());
int64_t top = 0, lft = 0, bot = matrix.size() - 1, rgt = matrix[0].size() - 1;
int64_t cur = 0;
while (bot >= top || rgt >= lft) {
if (bot >= top) {
for (cur = lft; cur <= rgt; ++cur) {
res.push_back(matrix[top][cur]);
}
}
++top;
if (rgt >= lft) {
for (cur = top; cur <= bot; ++cur) {
res.push_back(matrix[cur][rgt]);
}
}
--rgt;
if (bot >= top) {
for (cur = rgt; cur >= lft; --cur) {
res.push_back(matrix[bot][cur]);
}
}
--bot;
if (rgt >= lft) {
for (cur = bot; cur >= top; --cur) {
res.push_back(matrix[cur][lft]);
}
}
++lft;
}
return res;
}
};
int main(int argc, char const* argv[]) {
Solution solution;
vector<vector<int>> matrix0{
{1, 1, 1, 1, 1},
{2, 2, 2, 2, 2},
{3, 3, 3, 3, 3},
{4, 4, 4, 4, 4},
};
vector<vector<int>> matrix1{
{1},
{2},
{3},
{4},
};
for (auto&& value : solution.spiralOrder(matrix0)) {
cout << value << " ";
}
cout << endl;
for (auto&& value : solution.spiralOrder(matrix1)) {
cout << value << " ";
}
}