-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY0081.cpp
More file actions
31 lines (31 loc) · 950 Bytes
/
DAY0081.cpp
File metadata and controls
31 lines (31 loc) · 950 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
// 54. Spiral Matrix
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int left=0,right=matrix[0].size()-1,top=0,bottom=matrix.size()-1;
vector<int>answer;
while(left<=right&&top<=bottom){
for(int col=left;col<=right;col++){
answer.push_back(matrix[top][col]);
}
top++;
for(int row=top;row<=bottom;row++){
answer.push_back(matrix[row][right]);
}
right--;
if(top<=bottom){
for(int col=right;col>=left;col--){
answer.push_back(matrix[bottom][col]);
}
bottom--;
}
if(left<=right){
for(int row=bottom;row>=top;row--){
answer.push_back(matrix[row][left]);
}
left++;
}
}
return answer;
}
};