-
Notifications
You must be signed in to change notification settings - Fork 0
/
leet_0054_spiral_matrix__OLD.cc
61 lines (58 loc) · 1.44 KB
/
leet_0054_spiral_matrix__OLD.cc
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
55
56
57
58
59
60
61
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix)
{
int max_r = matrix.size(), max_c = matrix[0].size(), area;
area = max_r * max_c;
if (area == 1)
return matrix[0] ;
int r, c, count, min_r, min_c ;
vector<int> E ;
min_r = min_c = -1 ; // bottom row, bottom col
r = c = count = 0 ;
while (1)
{
if (count)
++ c ;
while (c < max_c)
{
E.push_back(matrix[r][c]);
++ c ;
++ count ;
}
if (count == area)
break ;
-- c ;
++ r ;
while (r < max_r)
{
E.push_back(matrix[r][c]);
++ r ;
++ count ;
}
if (count == area)
break ;
-- r ;
while (--c > min_c)
{
E.push_back(matrix[r][c]);
++ count ;
}
if (count == area)
break ;
++ c ;
-- max_r ;
-- max_c ;
++ min_r ;
++ min_c ;
while (--r > min_r)
{
E.push_back(matrix[r][c]);
++ count ;
}
if (count == area) break ;
++ r ;
}
return E ;
}
};