-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path54.spiral-matrix.cpp
More file actions
36 lines (30 loc) · 1.03 KB
/
54.spiral-matrix.cpp
File metadata and controls
36 lines (30 loc) · 1.03 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
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> v;
int right=matrix[0].size()-1, bottom=matrix.size()-1;
int left= 0, top=0;
int direction = 0;
while(v.size() != matrix.size()*matrix[0].size()){
if(direction == 0){
for(int i=left; i<=right; i++)
v.push_back(matrix[top][i]);
top++;
}else if(direction == 1){
for(int i=top; i<=bottom; i++)
v.push_back(matrix[i][right]);
right--;
}else if(direction == 2){
for(int i=right; i>=left; i--)
v.push_back(matrix[bottom][i]);
bottom--;
}else if(direction == 3){
for(int i=bottom; i>=top; i--)
v.push_back(matrix[i][left]);
left++;
}
direction = (direction+1)%4;
}
return v;
}
};