-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiralNumbers.java
52 lines (45 loc) · 1.11 KB
/
SpiralNumbers.java
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
/*
Construct a square matrix with a size N × N containing integers from 1 to N * N in a spiral order, starting from top-left and in clockwise direction.
Example
For n = 3, the output should be
spiralNumbers(n) = [[1, 2, 3],
[8, 9, 4],
[7, 6, 5]]
*/
int[][] spiralNumbers(int n) {
int res[][]=new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
res[i][j]=0;
}
}
boolean flag=true;
int i=0,j=-1;
for(int l=1;l<=n*n;l++){
if( flag && j+1<n && res[i][j+1]==0){
res[i][j+1]=l;
j+=1;
continue;
}
else if( flag && i+1<n && res[i+1][j]==0){
res[i+1][j]=l;
i+=1;
continue;
}
flag=false;
if( !flag && j-1>=0 && res[i][j-1]==0){
res[i][j-1]=l;
j-=1;
continue;
}
else if( !flag && i-1>=0 && res[i-1][j]==0){
res[i-1][j]=l;
i-=1;
continue;
}
flag=true;
res[i][j+1]=l;
j+=1;
}
return res;
}