-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiral_Matrix.cpp
More file actions
27 lines (27 loc) · 862 Bytes
/
Spiral_Matrix.cpp
File metadata and controls
27 lines (27 loc) · 862 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
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res;
if(matrix.size()==0)
return res;
int top = 0, bottom = matrix.size()-1;
int left = 0, right = matrix[0].size()-1;
while(left<=right && top<=bottom){
for(int c=left; c<=right; c++)
res.push_back(matrix[top][c]);
for(int r=top+1; r<=bottom; r++)
res.push_back(matrix[r][right]);
if(top<bottom && left<right){
for(int c=right-1; c>left; c--)
res.push_back(matrix[bottom][c]);
for(int r=bottom; r>top; r--)
res.push_back(matrix[r][left]);
}
top++;
bottom--;
left++;
right--;
}
return res;
}
};