0%

Leetcode059-spiralMatrixII

Description

Given a positive integer n, generate a square matrix filled with elements from 1 to $n^2$ in spiral order.

Example

1
2
3
4
5
6
7
Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]

Solution

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 int[][] generateMatrix(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;
int x = 0;
int y = 0;
int dir = 1;
for (int i = 1; i <= n*n; i++){
res[x][y] = i;
if (dir == 1){
if (y == n-1 || res[x][y+1] > 0){
x += 1;
dir = 2;
}
else y += 1;
}
else if (dir == 2){
if (x == n-1 || res[x+1][y] > 0){
y -= 1;
dir = 3;
}
else x += 1;
}
else if (dir == 3){
if (y == 0 || res[x][y-1] > 0){
x -= 1;
dir = 4;
}
else y -= 1;
}
else if (dir == 4){
if (x == 0 || res[x-1][y] > 0){
y += 1;
dir = 1;
}
else x -= 1;
}
}
return res;
}
}