-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathSpiral Matrix.cpp
43 lines (37 loc) · 920 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
lass Solution{
public:
int findK(vector<vector<int>> &matrix, int n, int m, int k)
{
// Your code goes here
int temp=m;
m=n;
n=temp;
int r=0;
int c=0;
int count=0;
vector<int>ans(m*n);
while(r<m && c<n){
for(int i=c;i<n;i++){
ans[count++]=matrix[r][i];
}
r++;
for(int i=r;i<m;i++){
ans[count++]=matrix[i][n-1];
}
n--;
if(r<m){
for(int i=n-1;i>=c;i--){
ans[count++]=matrix[m-1][i];
}
m--;
}
if(c<n){
for(int i=m-1;i>=r;i--){
ans[count++]=matrix[i][c];
}
c++;
}
}
return ans[k-1];
}
};