forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiral_matrix.cpp
More file actions
34 lines (34 loc) · 757 Bytes
/
spiral_matrix.cpp
File metadata and controls
34 lines (34 loc) · 757 Bytes
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
class Solution {
public:
bool chk(int ix,int iy,int m, int n)
{
if(ix<0||iy<0||ix==m||iy==n)
return true;
return false;
}
void cng(int &ix,int &iy)
{
swap(ix,iy);
if(ix==0)
iy*=-1;
return;
}
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ans;
int m=matrix.size();
int n=matrix[0].size();
int cnt=m*n;
int x=0,y=0;
int ix=0,iy=1;
while(cnt--)
{
if(chk(x+ix,y+iy,m,n)||matrix[x+ix][y+iy]==-101)
cng(ix,iy);
ans.push_back(matrix[x][y]);
matrix[x][y]=-101;
x+=ix;
y+=iy;
}
return ans;
}
};