-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspiral-matrix.cc
38 lines (32 loc) · 1.14 KB
/
spiral-matrix.cc
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
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
vector<int> res;
if(matrix.empty() || matrix.front().empty()) return res;
int m = matrix.size(), n = matrix.front().size();
int start = 0;
while(m > 1 && n > 1) {
for(int i=start; i<start+n-1; ++i)
res.push_back(matrix[start][i]);
for(int i=start; i<start+m-1; ++i)
res.push_back(matrix[i][start+n-1]);
for(int i=start+n-1; i>start; --i)
res.push_back(matrix[start+m-1][i]);
for(int i=start+m-1; i>start; --i)
res.push_back(matrix[i][start]);
++start;
m -= 2;
n -= 2;
}
if(m == 0 || n == 0) return res;
if(m == 1) {
for(int i=start; i<=start+n-1; ++i)
res.push_back(matrix[start][i]);
}
else if(n == 1) {
for(int i=start; i<=start+m-1; ++i)
res.push_back(matrix[i][start+n-1]);
}
return res;
}
};