forked from Dexter-99/Hacktober-Fest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiral Matrix.cpp
46 lines (43 loc) · 865 Bytes
/
Spiral Matrix.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
//question link: https://leetcode.com/problems/spiral-matrix/
//Please consider this under hacktober fest tag
class Solution {
public:
vector<int> spiralOrder( vector<vector<int>> A) {
int T,B,L,R,dir;
T=0;
B=A.size()-1;
L=0;
R=A[0].size()-1;
dir=0;
vector <int> ans;
int i;
while(T<=B && L<=R){
if(dir==0){
for( i=L; i<=R; i++){
ans.push_back(A[T][i]);
}
T++;
}
else if(dir==1){
for( i=T; i<=B; i++){
ans.push_back(A[i][R]);
}
R--;
}
else if(dir==2){
for( i=R; i>=L; i--){
ans.push_back(A[B][i]);
}
B--;
}
else if (dir==3){
for( i=B; i>=T; i--){
ans.push_back(A[i][L]);
}
L++;
}
dir=(dir+1)%4;
}
return ans;
}
};