-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#43.cc
More file actions
32 lines (26 loc) · 939 Bytes
/
LeetCode#43.cc
File metadata and controls
32 lines (26 loc) · 939 Bytes
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
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> ret;
int N = matrix.size();
if(N==0) return ret;
int M = matrix[0].size();
for(int i=0;i<(N+1)/2;i++)
if(i<=M-1-i){
for(int j=i;j<M-i;j++)
ret.push_back(matrix[i][j]);
if(i<N-1-i){
for(int j=i+1;j<N-1-i;j++)
ret.push_back(matrix[j][M-1-i]);
for(int j=M-1-i;j>=i;j--)
ret.push_back(matrix[N-1-i][j]);
if(i<M-1-i)
for(int j=N-i-2;j>i;j--)
ret.push_back(matrix[j][i]);
}
}
return ret;
}
};