/
Q54.cpp
78 lines (69 loc) · 2.21 KB
/
Q54.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
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> result;
if (matrix.size() == 0) return result;
int rows = matrix.size(), cols = matrix[0].size();
if (rows == 1){
for (int i = 0; i < cols; i++) result.push_back(matrix[0][i]);
return result;
}
if (cols == 1){
for (int i = 0; i < rows; i++) result.push_back(matrix[i][0]);
return result;
}
for (int i = 0; i < min(rows, cols) / 2; i++){
for (int j = i; j < cols - i - 1; j++){
result.push_back(matrix[i][j]);
}
for (int j = i; j < rows - i - 1; j++){
result.push_back(matrix[j][cols-i-1]);
}
for (int j = i; j < cols - i - 1; j++){
result.push_back(matrix[rows-i-1][cols - j - 1]);
}
for (int j = i; j < rows - i - 1; j++){
result.push_back(matrix[rows - j - 1][i]);
}
}
if (cols >= rows && rows % 2){
for (int i = rows / 2; i < cols - rows/2; i++){
result.push_back(matrix[rows/2][i]);
}
}
else if (cols < rows && cols % 2){
for (int i = cols / 2; i < rows - cols/2; i++){
result.push_back(matrix[i][cols/2]);
}
}
return result;
}
vector<int> spiralOrder_2(vector<vector<int>>& matrix){
vector<int> result; if (matrix.size() == 0) return result;
int left = 0, right = matrix[0].size() - 1, top = 0, bottom = matrix.size() - 1;
while (left <= right && top <= bottom){
for (int i = left; i < right; i++) result.push_back(matrix[top][i]);
for (int i = top; i <= bottom; i++) result.push_back(matrix[i][right]);
if (left < right && top < bottom){
for (int i = right - 1; i > left; i--) result.push_back(matrix[bottom][i]);
for (int i = bottom; i > top; i--) result.push_back(matrix[i][left]);
}
left++, right--, top++, bottom--;
}
return result;
}
};
int main(void){
vector<vector<int>> matrix = { { 1, 2, },
{ 5, 6, },
{ 9, 10 } };
vector<vector<int>> matrix2 = { { 1 }, { 2 }, { 3 } };
vector<vector<int>> matrix3 = { { 1, 2, 3 } };
Solution model;
//vector<int> result = model.spiralOrder(matrix);
vector<int> result = model.spiralOrder_2(matrix3);
return 0;
}