-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_54_Spiral.cpp
44 lines (34 loc) · 1.15 KB
/
LC_54_Spiral.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
/*
https://leetcode.com/problems/spiral-matrix/
*/
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int r = matrix.size();
int c = matrix[0].size();
vector<int> ans(r*c);
int i=0;
int j=0;
int ind=0;
int top=0;
int bottom=r-1;
int left=0;
int right=c-1;
while(top<=bottom && left<=right)
{
for(j=left; j<=right; j++ )
ans[ind++] = (matrix[top][j]);
if(++top>bottom)break; //delete top row and check
for(i=top; i<=bottom; i++)
ans[ind++] = (matrix[i][right]);
if(--right<left)break; // delete right col and check
for(j=right; j>=left; j--)
ans[ind++] = (matrix[bottom][j]);
if(--bottom<top)break; // delete bottom row and check
for(i=bottom; i>=top; i--)
ans[ind++] = (matrix[i][left]);
if(++left>right)break; // delete left col and check
}
return ans;
}//end
};