-
Notifications
You must be signed in to change notification settings - Fork 21
/
The Spiral Matrix II.java
executable file
·51 lines (46 loc) · 1.04 KB
/
The Spiral Matrix II.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
M
1534346957
tags: Array
#### Move forward till end
- Similar concept as `The Maze`: keep walking until hit wall, turn back
- fix direction `dx[direction % 4]`
```
/*
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
Example:
Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
*/
/*
Use while loop, reach end and stop
*/
class Solution {
int[] dx = {0, 1, 0, -1};
int[] dy = {1, 0, -1, 0};
public int[][] generateMatrix(int n) {
int[][] grid = new int[n][n];
int step = 1, i = 0, j = 0, direction = 0;
grid[i][j] = step++;
while (step <= n * n) {
int x = dx[direction % 4];
int y = dy[direction % 4];
i += x;
j += y;
while (i >= 0 && i < n && j >= 0 && j < n && grid[i][j] == 0) {
grid[i][j] = step++;
i += x;
j += y;
}
i -= x;
j -= y;
direction++;
}
return grid;
}
}
```