-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path54.cpp
More file actions
62 lines (51 loc) · 1.31 KB
/
54.cpp
File metadata and controls
62 lines (51 loc) · 1.31 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
/**
* 54. Spiral Matrix
* Given an m x n matrix, return all elements of the matrix in spiral order.
*/
#include <vector>
#include "common.h"
using namespace std;
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ret;
int m = matrix.size();
int n = matrix[0].size();
int count = 0;
int count_target = m * n;
int left = 0, right = n - 1;
int up = 0, down = m - 1;
while (count < count_target) {
for (int i = left; i <= right; ++i) {
ret.push_back(matrix[up][i]);
++count;
}
++up;
for (int i = up; i <= down; ++i) {
ret.push_back(matrix[i][right]);
++count;
}
--right;
for (int i = right; i >= left; --i) {
ret.push_back(matrix[down][i]);
++count;
}
--down;
for (int i = down; i >= up; --i) {
ret.push_back(matrix[i][left]);
++count;
}
++left;
}
ret.resize(count_target);
return ret;
}
int main() {
vector<vector<int>> matrix;
vector<int> ret;
matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
ret = spiralOrder(matrix);
printVec(ret);
matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
ret = spiralOrder(matrix);
printVec(ret);
return 0;
}