-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path54.spiral-matrix.cpp
More file actions
120 lines (109 loc) · 3.65 KB
/
54.spiral-matrix.cpp
File metadata and controls
120 lines (109 loc) · 3.65 KB
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
116
117
118
119
120
class Solution {
public:
inline void increment_col(std::vector<std::vector<int>>& matrix,
int& current_row, int& current_col, int& low_row,
int& high_row, int& low_col, int& high_col,
std::vector<int>& results, bool& possible) {
if (current_col > high_col) {
possible = false;
return;
}
while (current_col <= high_col) {
results.push_back(matrix[current_row][current_col]);
++current_col;
}
--current_col;
++current_row;
low_row = current_row;
return;
}
inline void increment_row(std::vector<std::vector<int>>& matrix,
int& current_row, int& current_col, int& low_row,
int& high_row, int& low_col, int& high_col,
std::vector<int>& results, bool& possible) {
if (current_row > high_row) {
possible = false;
return;
}
while (current_row <= high_row) {
results.push_back(matrix[current_row][current_col]);
++current_row;
}
--current_row;
--current_col;
high_col = current_col;
return;
}
inline void decrement_col(std::vector<std::vector<int>>& matrix,
int& current_row, int& current_col, int& low_row,
int& high_row, int& low_col, int& high_col,
std::vector<int>& results, bool& possible) {
if (current_col < low_col) {
possible = false;
return;
}
while (current_col >= low_col) {
results.push_back(matrix[current_row][current_col]);
--current_col;
}
++current_col;
--current_row;
high_row = current_row;
return;
}
inline void decrement_row(std::vector<std::vector<int>>& matrix,
int& current_row, int& current_col, int& low_row,
int& high_row, int& low_col, int& high_col,
std::vector<int>& results, bool& possible) {
std::cout << "decrement_row" << std::endl;
if (current_row < low_row) {
possible = false;
return;
}
while (current_row >= low_row) {
results.push_back(matrix[current_row][current_col]);
--current_row;
}
++current_row;
++current_col;
low_col = current_col;
return;
}
vector<int> spiralOrder(vector<vector<int>>& matrix) {
std::vector<int> results;
if (!matrix.size()) {
return results;
}
int low_row = 0;
int high_row = matrix.size() - 1;
int low_col = 0;
int high_col = matrix[0].size() - 1;
bool possible = true; // is it possible to continue
int current_row = 0;
int current_col = 0;
while (possible) {
increment_col(matrix, current_row, current_col, low_row, high_row,
low_col, high_col, results, possible);
std::cout << current_row << ", " << current_col << std::endl;
if (!possible) {
break;
}
increment_row(matrix, current_row, current_col, low_row, high_row,
low_col, high_col, results, possible);
std::cout << current_row << ", " << current_col << std::endl;
if (!possible) {
break;
}
decrement_col(matrix, current_row, current_col, low_row, high_row,
low_col, high_col, results, possible);
std::cout << current_row << ", " << current_col << std::endl;
if (!possible) {
break;
}
decrement_row(matrix, current_row, current_col, low_row, high_row,
low_col, high_col, results, possible);
std::cout << current_row << ", " << current_col << std::endl;
}
return results;
}
};