Skip to content

Commit 79b1507

Browse files
committed
Update solution 059 [Java]
1 parent 352b9ea commit 79b1507

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
6262
| 046 | [Permutations](https://github.com/doocs/leetcode/tree/master/solution/046.Permutations) | `Backtracking` |
6363
| 047 | [Permutations II](https://github.com/doocs/leetcode/tree/master/solution/047.Permutations%20II) | `Backtracking` |
6464
| 054 | [Spiral Matrix](https://github.com/doocs/leetcode/tree/master/solution/054.Spiral%20Matrix) | `Array` |
65+
| 059 | [Spiral Matrix II](https://github.com/doocs/leetcode/tree/master/solution/059.Spiral%20Matrix%20II) | `Array` |
6566
| 061 | [Rotate List](https://github.com/doocs/leetcode/tree/master/solution/061.Rotate%20List) | `Linked List`, `Two Pointers` |
6667
| 062 | [Unique Paths](https://github.com/doocs/leetcode/tree/master/solution/062.Unique%20Paths) | `Array`, `Dynamic Programming` |
6768
| 063 | [Unique Paths II](https://github.com/doocs/leetcode/tree/master/solution/063.Unique%20Paths%20II) | `Array`, `Dynamic Programming` |
@@ -104,8 +105,8 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http
104105
" If you want to go fast, go alone. If you want to go far, go together. And that's the spirit of [teamwork](https://github.com/doocs/leetcode/graphs/contributors) ".
105106

106107
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
107-
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="80px;"/>](https://github.com/yanglbme)<br />[💻](https://github.com/doocs/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="80px;"/>](https://github.com/chakyam)<br />[💻](https://github.com/doocs/leetcode/commits?author=chakyam "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/10081554?v=4" width="80px;"/>](https://github.com/zhkmxx9302013)<br />[💻](https://github.com/doocs/leetcode/commits?author=zhkmxx9302013 "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/40383345?v=4" width="80px;"/>](https://github.com/MarkKuang1991)<br />[💻](https://github.com/doocs/leetcode/commits?author=MarkKuang1991 "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/12371194?v=4" width="80px;"/>](https://github.com/fonxian)<br />[💻](https://github.com/doocs/leetcode/commits?author=fonxian "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/25222367?v=4" width="80px;"/>](https://github.com/zhanary)<br />[💻](https://github.com/doocs/leetcode/commits?author=zhanary "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/42396616?v=4" width="80px;"/>](https://github.com/ZhouTingZhaobiu)<br />[💻](https://github.com/doocs/leetcode/commits?author=ZhouTingZhaobiu "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/31923541?v=4" width="80px;"/>](https://github.com/zouwx2cs)<br />[💻](https://github.com/doocs/leetcode/commits?author=zouwx2cs "Code") </center> |
108+
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="80px;"/>](https://github.com/yanglbme) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="80px;"/>](https://github.com/chakyam) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/10081554?v=4" width="80px;"/>](https://github.com/zhkmxx9302013) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/40383345?v=4" width="80px;"/>](https://github.com/MarkKuang1991) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/12371194?v=4" width="80px;"/>](https://github.com/fonxian) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/25222367?v=4" width="80px;"/>](https://github.com/zhanary) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/42396616?v=4" width="80px;"/>](https://github.com/ZhouTingZhaobiu) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/31923541?v=4" width="80px;"/>](https://github.com/zouwx2cs) </center> |
108109
|---|---|---|---|---|---|---|---|
109-
| <center> [<img src="https://avatars3.githubusercontent.com/u/20679510?v=4" width="80px;"/>](https://github.com/Mrzhudky)<br />[💻](https://github.com/doocs/leetcode/commits?author=Mrzhudky "Code") </center> |
110+
| <center> [<img src="https://avatars3.githubusercontent.com/u/20679510?v=4" width="80px;"/>](https://github.com/Mrzhudky) </center> |
110111

111112
<!-- ALL-CONTRIBUTORS-LIST:END -->
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## 螺旋矩阵 II
2+
### 题目描述
3+
4+
给定一个正整数 n,生成一个包含 1 到 n² 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。
5+
6+
**示例:**
7+
```
8+
输入: 3
9+
输出:
10+
[
11+
[ 1, 2, 3 ],
12+
[ 8, 9, 4 ],
13+
[ 7, 6, 5 ]
14+
]
15+
```
16+
17+
### 解法
18+
定义一个变量 `val`,由外往里,一圈圈遍历矩阵,进行赋值,每次赋值后 `val++`
19+
20+
21+
```java
22+
class Solution {
23+
public int[][] generateMatrix(int n) {
24+
if (n < 1) {
25+
return null;
26+
}
27+
28+
int[][] res = new int[n][n];
29+
int val = 1;
30+
31+
int m1 = 0;
32+
int m2 = n - 1;
33+
while (m1 < m2) {
34+
for (int j = m1; j < m2; ++j) {
35+
res[m1][j] = val++;
36+
}
37+
for (int i = m1; i < m2; ++i) {
38+
res[i][m2] = val++;
39+
}
40+
for (int j = m2; j > m1; --j) {
41+
res[m2][j] = val++;
42+
}
43+
for (int i = m2; i > m1; --i) {
44+
res[i][m1] = val++;
45+
}
46+
++m1;
47+
--m2;
48+
}
49+
if (m1 == m2) {
50+
res[m1][m1] = val;
51+
}
52+
53+
return res;
54+
}
55+
}
56+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public int[][] generateMatrix(int n) {
3+
if (n < 1) {
4+
return null;
5+
}
6+
7+
int[][] res = new int[n][n];
8+
int val = 1;
9+
10+
int m1 = 0;
11+
int m2 = n - 1;
12+
while (m1 < m2) {
13+
for (int j = m1; j < m2; ++j) {
14+
res[m1][j] = val++;
15+
}
16+
for (int i = m1; i < m2; ++i) {
17+
res[i][m2] = val++;
18+
}
19+
for (int j = m2; j > m1; --j) {
20+
res[m2][j] = val++;
21+
}
22+
for (int i = m2; i > m1; --i) {
23+
res[i][m1] = val++;
24+
}
25+
++m1;
26+
--m2;
27+
}
28+
if (m1 == m2) {
29+
res[m1][m1] = val;
30+
}
31+
32+
return res;
33+
}
34+
}

0 commit comments

Comments
 (0)