-
Notifications
You must be signed in to change notification settings - Fork 0
/
191231-1.cpp
98 lines (95 loc) · 1.66 KB
/
191231-1.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
// https://leetcode-cn.com/problems/spiral-matrix/
#include <cstdio>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> a;
int h = matrix.size();
int w = (h <= 0 ? 0 : matrix.front().size());
visit(a, matrix, w, h, 0, 0);
return a;
}
private:
void visit(vector<int>& a, const vector<vector<int>>& m, int w, int h, int x, int y)
{
if (h <= 0 || w <= 0) return;
if (h == 1) {
for (int i = 0; i < w; ++i) {
a.push_back(m[y][x + i]);
}
} else if (w == 1) {
for (int i = 0; i < h; ++i) {
a.push_back(m[y + i][x]);
}
} else {
for (int i = 0; i < w - 1; ++i) {
a.push_back(m[y][x + i]);
}
for (int i = 0; i < h - 1; ++i) {
a.push_back(m[y + i][x + w - 1]);
}
for (int i = w - 1; i > 0; --i) {
a.push_back(m[y + h - 1][x + i]);
}
for (int i = h - 1; i > 0; --i) {
a.push_back(m[y + i][x]);
}
visit(a, m, w - 2, h - 2, x + 1, y + 1);
}
}
};
void print(const vector<int>& a)
{
printf("[");
for (const auto& e : a) {
printf(" %d", e);
}
printf(" ]\n");
}
int main()
{
Solution s;
{
vector<vector<int>> a = {
{1,2,3},
{4,5,6},
{7,8,9}
};
print(s.spiralOrder(a));
}
{
vector<vector<int>> a = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};
print(s.spiralOrder(a));
}
{
vector<vector<int>> a = {
{1,2,3,4},
};
print(s.spiralOrder(a));
}
{
vector<vector<int>> a = {
};
print(s.spiralOrder(a));
}
{
vector<vector<int>> a = {
{1}, {2}, {3}, {4}
};
print(s.spiralOrder(a));
}
{
vector<vector<int>> a = {
{1,2,3,4,5},
{6,7,8,9,10}
};
print(s.spiralOrder(a));
}
return 0;
}