-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
54 lines (51 loc) · 1.27 KB
/
solution.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
struct Node
{
int x,y;
Node(int _x,int _y):x(_x),y(_y){}
};
class Solution
{
public:
vector<int> spiralOrder(vector<vector<int> > &matrix)
{
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> result;
int m=matrix.size();
if(m==0)
return result;
int n=matrix[0].size();
if(n==0)
return result;
Node a(0,0);
Node b(m-1,n-1);
int e = m < n ? m : n;
e = e/2;
while(e--)
{
for(int i=a.y;i<=b.y;i++)
result.push_back(matrix[a.x][i]);
for(int i=a.x+1;i<=b.x;i++)
result.push_back(matrix[i][b.y]);
for(int i=b.y-1;i>=a.y;i--)
result.push_back(matrix[b.x][i]);
for(int i=b.x-1;i>a.x;--i)
result.push_back(matrix[i][a.y]);
a.x++;
a.y++;
b.x--;
b.y--;
}
if(a.x==b.x)
{
for(int i=a.y;i<=b.y;i++)
result.push_back(matrix[a.x][i]);
}
else if(a.y==b.y)
{
for(int i=a.x;i<=b.x;i++)
result.push_back(matrix[i][a.y]);
}
return result;
}
};