-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixSpiralCopy.cpp
43 lines (40 loc) · 1.3 KB
/
matrixSpiralCopy.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
/* Coding challenge #54 from LeetCode */
/* Improved code from previous version */
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> result;
if (matrix.empty()) {
return result;
}
int start_row = 0;
int end_row = matrix.size() - 1;
int start_column = 0;
int end_column = matrix[0].size()-1;
while (start_row <= end_row && start_column <= end_column) {
for (int j = start_column; j <= end_column; ++j) {
result.push_back(matrix[start_row][j]);
}
start_row++;
if (start_row > end_row) {
break;
}
for (int i = start_row; i <= end_row; i++) {
result.push_back(matrix[i][end_column]);
}
end_column--;
if (start_column > end_column) {
break;
}
for (int j = end_column; j >= start_column; j--) {
result.push_back(matrix[end_row][j]);
}
end_row--;
for (int i = end_row; i >= start_row; i--) {
result.push_back(matrix[i][start_column]);
}
start_column++;
}
return result;
}
};