54. 螺旋矩阵
为保证权益,题目请参考 54. 螺旋矩阵(From LeetCode).
解决方案1
CPP
C++
//
// Created by lenovo on 2020-10-01.
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>> &matrix) {
vector<int> ans;
if (matrix.empty() || matrix[0].empty()) {
return ans;
}
// layer
for (int i = 0; i <= (min(matrix.size(),matrix[0].size()) - 1) / 2; i++) {
int left = i;
int right = matrix[0].size() - 1 - i;
int top = i;
int bottom = matrix.size() - 1 - i;
if(left == right){
if(top == bottom){
ans.push_back(matrix[left][top]);
}else{
// right
for (int j = top; j <= bottom; j++) {
ans.push_back(matrix[j][right]);
}
break;
}
}else{
if(top == bottom){
// top
for (int j = left; j <= right; j++) {
ans.push_back(matrix[top][j]);
}
}
else {
// top
for (int j = left; j < right; j++) {
ans.push_back(matrix[top][j]);
}
// right
for (int j = top; j < bottom; j++) {
ans.push_back(matrix[j][right]);
}
// bottom
for (int j = right; j > left; j--) {
ans.push_back(matrix[bottom][j]);
}
// left
for (int j = bottom; j> top;j--){
ans.push_back(matrix[j][left]);
}
}
}
}
return ans;
}
};
void runTest() {
vector<vector<int>> vec;
vector<int> tmp;
tmp.push_back(1);
vec.push_back(tmp);
vector<int> tmp2;
tmp2.push_back(5);
vec.push_back(tmp2);
vector<int> tmp3;
tmp3.push_back(9);
vec.push_back(tmp3);
vector<int> tmp4;
tmp4.push_back(9);
vec.push_back(tmp4);
vector<int> tmp5;
tmp5.push_back(9);
vec.push_back(tmp5);
vector<int> tmp6;
tmp6.push_back(9);
vec.push_back(tmp6);
vector<int> tmp7;
tmp7.push_back(9);
vec.push_back(tmp7);
vector<int> tmp8;
tmp8.push_back(9);
vec.push_back(tmp8);
Solution so;
for (auto x : so.spiralOrder(vec)){
cout << x << " ";
}
}
int main() {
runTest();
return 0;
}
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
104
105
106
107
108
109
110
111
112
113
114
115
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
104
105
106
107
108
109
110
111
112
113
114
115