-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiraltraversal.cpp
More file actions
31 lines (30 loc) · 801 Bytes
/
spiraltraversal.cpp
File metadata and controls
31 lines (30 loc) · 801 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
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int rb=0,re=matrix.size()-1;
int cb=0,ce=matrix[0].size()-1;
vector<int>v;
while(rb<=re && cb<=ce)
{
for(int i=cb;i<=ce;i++)
v.push_back(matrix[rb][i]);
rb++;
for(int i=rb;i<=re;i++)
v.push_back(matrix[i][ce]);
ce--;
if(rb<=re)
{
for(int i=ce;i>=cb;i--)
v.push_back(matrix[re][i]);
re--;
}
if(cb<=ce)
{
for(int i=re;i>=rb;i--)
v.push_back(matrix[i][cb]);
cb++;
}
}
return v;
}
};