Skip to content

Commit caedf40

Browse files
committed
59. Spiral Matrix II
1 parent f6d30f2 commit caedf40

File tree

4 files changed

+328
-0
lines changed

4 files changed

+328
-0
lines changed

markdown/59. Spiral Matrix II.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
### [59\. Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a positive integer _n_, generate a square matrix filled with elements from 1 to _n_<sup>2</sup> in spiral order.
7+
8+
**Example:**
9+
10+
```
11+
Input: 3
12+
Output:
13+
[
14+
[ 1, 2, 3 ],
15+
[ 8, 9, 4 ],
16+
[ 7, 6, 5 ]
17+
]
18+
```
19+
20+
21+
#### MySolution
22+
23+
Language: **Java**
24+
25+
```java
26+
class Solution {
27+
public int[][] generateMatrix(int n) {
28+
if (n == 0) {
29+
return new int[0][0];
30+
}
31+
int[][] matrix = new int[n][n];
32+
int x = 0; // 需要填充的坐标
33+
int y = 0; // 需要填充的坐标
34+
int direction = 0; // 0-右 1-下 2-左 3-上
35+
for (int i = 1; i <= n * n; i++) {
36+
matrix[x][y] = i; //
37+
// 判断下一个位置走向,索引定位到下一个位置
38+
switch (direction) {
39+
case 0:
40+
if (available(matrix, n, x, y + 1)) {
41+
y++;
42+
} else {
43+
direction = 1;
44+
x++;
45+
}
46+
break;
47+
case 1:
48+
if (available(matrix, n, x + 1, y)) {
49+
x++;
50+
} else {
51+
direction = 2;
52+
y--;
53+
}
54+
break;
55+
case 2:
56+
if (available(matrix, n, x, y - 1)) {
57+
y--;
58+
} else {
59+
direction = 3;
60+
x--;
61+
}
62+
break;
63+
case 3:
64+
if (available(matrix, n, x - 1, y)) {
65+
x--;
66+
} else {
67+
direction = 0;
68+
y++;
69+
}
70+
break;
71+
}
72+
}
73+
return matrix;
74+
}
75+
76+
/**
77+
* 判断坐标是否可用
78+
*
79+
* @param n 大小
80+
* @param x 横坐标
81+
* @param y 纵坐标
82+
* @return 是否可用
83+
*/
84+
private boolean available(int[][] matrix, int n, int x, int y) {
85+
return !(x >= n || y >= n || x < 0 || y < 0 || matrix[x][y] > 0);
86+
}
87+
}
88+
```
89+
90+
#### Recommend Solution
91+
92+
Language: **Java**
93+
94+
```java
95+
class Solution {
96+
public static int[][] generateMatrix(int n) {
97+
int[][] ret = new int[n][n];
98+
int left = 0, top = 0;
99+
int right = n - 1, down = n - 1;
100+
int count = 1;
101+
while (left <= right) {
102+
for (int j = left; j <= right; j++) {
103+
ret[top][j] = count++;
104+
}
105+
top++;
106+
for (int i = top; i <= down; i++) {
107+
ret[i][right] = count++;
108+
}
109+
right--;
110+
for (int j = right; j >= left; j--) {
111+
ret[down][j] = count++;
112+
}
113+
down--;
114+
for (int i = down; i >= top; i--) {
115+
ret[i][left] = count++;
116+
}
117+
left++;
118+
}
119+
return ret;
120+
}
121+
}
122+
```
123+
![](http://ww4.sinaimg.cn/large/006tNc79ly1g4yndj6hpfj31bk0py431.jpg)

src/main/java/leetcode/_59_/Main.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode._59_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
int[][] ints = solution.generateMatrix(0);
10+
for (int i = 0; i < ints.length; i++) {
11+
for (int j = 0; j < ints[i].length; j++) {
12+
System.out.print(ints[i][j] + ",");
13+
}
14+
System.out.println();
15+
}
16+
}
17+
}
18+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package leetcode._59_;
2+
3+
class Solution {
4+
public int[][] generateMatrix(int n) {
5+
if (n == 0) {
6+
return new int[0][0];
7+
}
8+
int[][] matrix = new int[n][n];
9+
int x = 0; // 需要填充的坐标
10+
int y = 0; // 需要填充的坐标
11+
int direction = 0; // 0-右 1-下 2-左 3-上
12+
for (int i = 1; i <= n * n; i++) {
13+
matrix[x][y] = i; // 写
14+
// 判断下一个位置走向,索引定位到下一个位置
15+
switch (direction) {
16+
case 0:
17+
if (available(matrix, n, x, y + 1)) {
18+
y++;
19+
} else {
20+
direction = 1;
21+
x++;
22+
}
23+
break;
24+
case 1:
25+
if (available(matrix, n, x + 1, y)) {
26+
x++;
27+
} else {
28+
direction = 2;
29+
y--;
30+
}
31+
break;
32+
case 2:
33+
if (available(matrix, n, x, y - 1)) {
34+
y--;
35+
} else {
36+
direction = 3;
37+
x--;
38+
}
39+
break;
40+
case 3:
41+
if (available(matrix, n, x - 1, y)) {
42+
x--;
43+
} else {
44+
direction = 0;
45+
y++;
46+
}
47+
break;
48+
}
49+
}
50+
return matrix;
51+
}
52+
53+
/**
54+
* 判断坐标是否可用
55+
*
56+
* @param n 大小
57+
* @param x 横坐标
58+
* @param y 纵坐标
59+
* @return 是否可用
60+
*/
61+
private boolean available(int[][] matrix, int n, int x, int y) {
62+
return !(x >= n || y >= n || x < 0 || y < 0 || matrix[x][y] > 0);
63+
}
64+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
### [59\. Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a positive integer _n_, generate a square matrix filled with elements from 1 to _n_<sup>2</sup> in spiral order.
7+
8+
**Example:**
9+
10+
```
11+
Input: 3
12+
Output:
13+
[
14+
[ 1, 2, 3 ],
15+
[ 8, 9, 4 ],
16+
[ 7, 6, 5 ]
17+
]
18+
```
19+
20+
21+
#### MySolution
22+
23+
Language: **Java**
24+
25+
```java
26+
class Solution {
27+
public int[][] generateMatrix(int n) {
28+
if (n == 0) {
29+
return new int[0][0];
30+
}
31+
int[][] matrix = new int[n][n];
32+
int x = 0; // 需要填充的坐标
33+
int y = 0; // 需要填充的坐标
34+
int direction = 0; // 0-右 1-下 2-左 3-上
35+
for (int i = 1; i <= n * n; i++) {
36+
matrix[x][y] = i; //
37+
// 判断下一个位置走向,索引定位到下一个位置
38+
switch (direction) {
39+
case 0:
40+
if (available(matrix, n, x, y + 1)) {
41+
y++;
42+
} else {
43+
direction = 1;
44+
x++;
45+
}
46+
break;
47+
case 1:
48+
if (available(matrix, n, x + 1, y)) {
49+
x++;
50+
} else {
51+
direction = 2;
52+
y--;
53+
}
54+
break;
55+
case 2:
56+
if (available(matrix, n, x, y - 1)) {
57+
y--;
58+
} else {
59+
direction = 3;
60+
x--;
61+
}
62+
break;
63+
case 3:
64+
if (available(matrix, n, x - 1, y)) {
65+
x--;
66+
} else {
67+
direction = 0;
68+
y++;
69+
}
70+
break;
71+
}
72+
}
73+
return matrix;
74+
}
75+
76+
/**
77+
* 判断坐标是否可用
78+
*
79+
* @param n 大小
80+
* @param x 横坐标
81+
* @param y 纵坐标
82+
* @return 是否可用
83+
*/
84+
private boolean available(int[][] matrix, int n, int x, int y) {
85+
return !(x >= n || y >= n || x < 0 || y < 0 || matrix[x][y] > 0);
86+
}
87+
}
88+
```
89+
90+
#### Recommend Solution
91+
92+
Language: **Java**
93+
94+
```java
95+
class Solution {
96+
public static int[][] generateMatrix(int n) {
97+
int[][] ret = new int[n][n];
98+
int left = 0, top = 0;
99+
int right = n - 1, down = n - 1;
100+
int count = 1;
101+
while (left <= right) {
102+
for (int j = left; j <= right; j++) {
103+
ret[top][j] = count++;
104+
}
105+
top++;
106+
for (int i = top; i <= down; i++) {
107+
ret[i][right] = count++;
108+
}
109+
right--;
110+
for (int j = right; j >= left; j--) {
111+
ret[down][j] = count++;
112+
}
113+
down--;
114+
for (int i = down; i >= top; i--) {
115+
ret[i][left] = count++;
116+
}
117+
left++;
118+
}
119+
return ret;
120+
}
121+
}
122+
```
123+
![](http://ww4.sinaimg.cn/large/006tNc79ly1g4yndj6hpfj31bk0py431.jpg)

0 commit comments

Comments
 (0)