-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path59_Spiral_Matrix_II.cc
43 lines (42 loc) · 1.14 KB
/
59_Spiral_Matrix_II.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
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
if(n==0){
vector<vector<int>> res0;
return res0;
}
vector<vector<int>> res(n,vector<int>(n,0));
int i=0,j=0;
res[i][j]=1;
while(res[i][j]!=n*n){
if(j+1<n&&res[i][j+1]==0){
for(j=j+1;j<n&&res[i][j]==0;++j){
res[i][j]=res[i][j-1]+1;
}
j--;
}
else if(i+1<n&&res[i+1][j]==0){
for(i=i+1;i<n&&res[i][j]==0;++i){
res[i][j]=res[i-1][j]+1;
}
i--;
}
else if(j-1>=0&&res[i][j-1]==0){
for(j=j-1;j>=0&&res[i][j]==0;--j){
res[i][j]=res[i][j+1]+1;
}
j++;
}
else if(i-1>=0&&res[i-1][j]==0){
for(i=i-1;i>=0&&res[i][j]==0;--i){
res[i][j]=res[i+1][j]+1;
}
i++;
}
else{
continue;
}
}
return res;
}
};