-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet108.java
More file actions
31 lines (30 loc) · 856 Bytes
/
leet108.java
File metadata and controls
31 lines (30 loc) · 856 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
vector<int> spirallyTraverse(vector<vector<int> > &mat) {
int n=mat.size();
int m=mat[0].size();
int left=0,right=m-1;
int top=0, bottom=n-1;
vector<int>ans;
while(left<=right && top<=bottom){
for(int i=left;i<=right;i++){
ans.push_back(mat[top][i]);
}
top++;
for(int i=top;i<=bottom;i++){
ans.push_back(mat[i][right]);
}
right--;
if(top<=bottom){
for(int i=right;i>=left;i--){
ans.push_back(mat[bottom][i]);
}
bottom--;
}
if(left<=right){
for(int i=bottom;i>=top;i--){
ans.push_back(mat[i][left]);
}
left++;
}
}
return ans;
}