-
Notifications
You must be signed in to change notification settings - Fork 0
/
54. Spiral Matrix.txt
63 lines (62 loc) · 1.81 KB
/
54. Spiral Matrix.txt
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
62
63
//changing the value to INT_MAX is a bit unorthodox and is not appreciated
//but hey its working fine
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res;
int hor=matrix[0].size();
int ver=matrix.size()-1;
int a = -1,b = 0;
int flag1=0,flag2=0,flag3=0,flag4=0;
while(hor!=0 || ver!=0){
if(hor!=0){
a++;
for(int i=0;i<hor;i++){
if(matrix[b][a]!=INT_MAX)
res.push_back(matrix[b][a]);
matrix[b][a]=INT_MAX;
a++;
}
a--;
hor--;
}
if(hor==0 && ver==0) break;
if(ver!=0){
b++;
for(int i = 0;i<ver;i++){
if(matrix[b][a]!=INT_MAX)
res.push_back(matrix[b][a]);
matrix[b][a]=INT_MAX;
b++;
}
b--;
ver--;
}
if(hor==0 && ver==0) break;
if(hor!=0){
a--;
for(int i=0;i<hor;i++){
if(matrix[b][a]!=INT_MAX)
res.push_back(matrix[b][a]);
matrix[b][a]=INT_MAX;
a--;
}
a++;
hor--;
}
if(hor==0 && ver==0) break;
if(ver!=0){
b--;
for(int i = 0;i<ver;i++){
if(matrix[b][a]!=INT_MAX)
res.push_back(matrix[b][a]);
matrix[b][a]=INT_MAX;
b--;
}
b++;
ver--;
}
}
return res;
}
};