Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6ae0a1b

Browse files
committed
feat: update solutions to leetcode problem: No.0059
1 parent 3970415 commit 6ae0a1b

File tree

5 files changed

+217
-73
lines changed

5 files changed

+217
-73
lines changed

‎solution/0000-0099/0059.Spiral Matrix II/README.md‎

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,97 @@
2828
<!-- 这里可写当前语言的特殊实现逻辑 -->
2929

3030
```python
31-
31+
class Solution:
32+
def generateMatrix(self, n: int) -> List[List[int]]:
33+
res = [[0] * n for _ in range(n)]
34+
num = 1
35+
m1, m2 = 0, n - 1
36+
while m1 < m2:
37+
for j in range(m1, m2):
38+
res[m1][j] = num
39+
num += 1
40+
for i in range(m1, m2):
41+
res[i][m2] = num
42+
num += 1
43+
for j in range(m2, m1, -1):
44+
res[m2][j] = num
45+
num += 1
46+
for i in range(m2, m1, -1):
47+
res[i][m1] = num
48+
num += 1
49+
m1 += 1
50+
m2 -= 1
51+
if m1 == m2:
52+
res[m1][m1] = num
53+
return res
3254
```
3355

3456
### **Java**
3557

3658
<!-- 这里可写当前语言的特殊实现逻辑 -->
3759

3860
```java
61+
class Solution {
62+
public int[][] generateMatrix(int n) {
63+
int[][] res = new int[n][n];
64+
int num = 1;
65+
int m1 = 0, m2 = n - 1;
66+
while (m1 < m2) {
67+
for (int j = m1; j < m2; ++j) {
68+
res[m1][j] = num++;
69+
}
70+
for (int i = m1; i < m2; ++i) {
71+
res[i][m2] = num++;
72+
}
73+
for (int j = m2; j > m1; --j) {
74+
res[m2][j] = num++;
75+
}
76+
for (int i = m2; i > m1; --i) {
77+
res[i][m1] = num++;
78+
}
79+
++m1;
80+
--m2;
81+
}
82+
if (m1 == m2) {
83+
res[m1][m1] = num;
84+
}
85+
86+
return res;
87+
}
88+
}
89+
```
3990

91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
vector<vector<int>> generateMatrix(int n) {
97+
vector<vector<int>> res(n, vector<int>(n, 0));
98+
int num = 1;
99+
int m1 = 0, m2 = n - 1;
100+
while (m1 < m2) {
101+
for (int j = m1; j < m2; ++j) {
102+
res[m1][j] = num++;
103+
}
104+
for (int i = m1; i < m2; ++i) {
105+
res[i][m2] = num++;
106+
}
107+
for (int j = m2; j > m1; --j) {
108+
res[m2][j] = num++;
109+
}
110+
for (int i = m2; i > m1; --i) {
111+
res[i][m1] = num++;
112+
}
113+
++m1;
114+
--m2;
115+
}
116+
if (m1 == m2) {
117+
res[m1][m1] = num;
118+
}
119+
return res;
120+
}
121+
};
40122
```
41123
42124
### **...**

‎solution/0000-0099/0059.Spiral Matrix II/README_EN.md‎

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,95 @@
3333
### **Python3**
3434

3535
```python
36-
36+
class Solution:
37+
def generateMatrix(self, n: int) -> List[List[int]]:
38+
res = [[0] * n for _ in range(n)]
39+
num = 1
40+
m1, m2 = 0, n - 1
41+
while m1 < m2:
42+
for j in range(m1, m2):
43+
res[m1][j] = num
44+
num += 1
45+
for i in range(m1, m2):
46+
res[i][m2] = num
47+
num += 1
48+
for j in range(m2, m1, -1):
49+
res[m2][j] = num
50+
num += 1
51+
for i in range(m2, m1, -1):
52+
res[i][m1] = num
53+
num += 1
54+
m1 += 1
55+
m2 -= 1
56+
if m1 == m2:
57+
res[m1][m1] = num
58+
return res
3759
```
3860

3961
### **Java**
4062

4163
```java
64+
class Solution {
65+
public int[][] generateMatrix(int n) {
66+
int[][] res = new int[n][n];
67+
int num = 1;
68+
int m1 = 0, m2 = n - 1;
69+
while (m1 < m2) {
70+
for (int j = m1; j < m2; ++j) {
71+
res[m1][j] = num++;
72+
}
73+
for (int i = m1; i < m2; ++i) {
74+
res[i][m2] = num++;
75+
}
76+
for (int j = m2; j > m1; --j) {
77+
res[m2][j] = num++;
78+
}
79+
for (int i = m2; i > m1; --i) {
80+
res[i][m1] = num++;
81+
}
82+
++m1;
83+
--m2;
84+
}
85+
if (m1 == m2) {
86+
res[m1][m1] = num;
87+
}
88+
89+
return res;
90+
}
91+
}
92+
```
4293

94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
vector<vector<int>> generateMatrix(int n) {
100+
vector<vector<int>> res(n, vector<int>(n, 0));
101+
int num = 1;
102+
int m1 = 0, m2 = n - 1;
103+
while (m1 < m2) {
104+
for (int j = m1; j < m2; ++j) {
105+
res[m1][j] = num++;
106+
}
107+
for (int i = m1; i < m2; ++i) {
108+
res[i][m2] = num++;
109+
}
110+
for (int j = m2; j > m1; --j) {
111+
res[m2][j] = num++;
112+
}
113+
for (int i = m2; i > m1; --i) {
114+
res[i][m1] = num++;
115+
}
116+
++m1;
117+
--m2;
118+
}
119+
if (m1 == m2) {
120+
res[m1][m1] = num;
121+
}
122+
return res;
123+
}
124+
};
43125
```
44126
45127
### **...**
Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,28 @@
11
class Solution {
22
public:
33
vector<vector<int>> generateMatrix(int n) {
4-
if (0 == n)
5-
return vector<vector<int>>() ;
6-
vector<vector<int>> res(n, vector<int>(n, 0)) ;
7-
8-
int i = 0, j = 0 ;
9-
10-
int dir = 0 ;
11-
12-
int n2 = n*n ;
13-
14-
for (int d = 1; d <= n2; ++d)
15-
{
16-
res[i][j] = d ;
17-
//cout << d << endl ;
18-
switch (dir)
19-
{
20-
case 0:
21-
++j ;
22-
if (j >= n || res[i][j])
23-
{
24-
dir++ ;
25-
--j ;
26-
++i ;
27-
}
28-
break ;
29-
case 1:
30-
++i ;
31-
if (i >= n || res[i][j])
32-
{
33-
dir++ ;
34-
--i ;
35-
--j ;
36-
}
37-
break ;
38-
case 2:
39-
--j ;
40-
if (j < 0 || res[i][j])
41-
{
42-
dir++ ;
43-
++j ;
44-
--i ;
45-
}
46-
break ;
47-
case 3:
48-
--i ;
49-
if (i < 0 || res[i][j])
50-
{
51-
dir = 0 ;
52-
++i ;
53-
++j ;
54-
}
55-
56-
break ;
57-
default:
58-
break ;
4+
vector<vector<int>> res(n, vector<int>(n, 0));
5+
int num = 1;
6+
int m1 = 0, m2 = n - 1;
7+
while (m1 < m2) {
8+
for (int j = m1; j < m2; ++j) {
9+
res[m1][j] = num++;
5910
}
60-
11+
for (int i = m1; i < m2; ++i) {
12+
res[i][m2] = num++;
13+
}
14+
for (int j = m2; j > m1; --j) {
15+
res[m2][j] = num++;
16+
}
17+
for (int i = m2; i > m1; --i) {
18+
res[i][m1] = num++;
19+
}
20+
++m1;
21+
--m2;
22+
}
23+
if (m1 == m2) {
24+
res[m1][m1] = num;
6125
}
62-
63-
return res ;
26+
return res;
6427
}
6528
};

‎solution/0000-0099/0059.Spiral Matrix II/Solution.java‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
class Solution {
22
public int[][] generateMatrix(int n) {
3-
if (n < 1) {
4-
return null;
5-
}
6-
73
int[][] res = new int[n][n];
8-
int val = 1;
9-
10-
int m1 = 0;
11-
int m2 = n - 1;
4+
int num = 1;
5+
int m1 = 0, m2 = n - 1;
126
while (m1 < m2) {
137
for (int j = m1; j < m2; ++j) {
14-
res[m1][j] = val++;
8+
res[m1][j] = num++;
159
}
1610
for (int i = m1; i < m2; ++i) {
17-
res[i][m2] = val++;
11+
res[i][m2] = num++;
1812
}
1913
for (int j = m2; j > m1; --j) {
20-
res[m2][j] = val++;
14+
res[m2][j] = num++;
2115
}
2216
for (int i = m2; i > m1; --i) {
23-
res[i][m1] = val++;
17+
res[i][m1] = num++;
2418
}
2519
++m1;
2620
--m2;
2721
}
2822
if (m1 == m2) {
29-
res[m1][m1] = val;
23+
res[m1][m1] = num;
3024
}
3125

3226
return res;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def generateMatrix(self, n: int) -> List[List[int]]:
3+
res = [[0] * n for _ in range(n)]
4+
num = 1
5+
m1, m2 = 0, n - 1
6+
while m1 < m2:
7+
for j in range(m1, m2):
8+
res[m1][j] = num
9+
num += 1
10+
for i in range(m1, m2):
11+
res[i][m2] = num
12+
num += 1
13+
for j in range(m2, m1, -1):
14+
res[m2][j] = num
15+
num += 1
16+
for i in range(m2, m1, -1):
17+
res[i][m1] = num
18+
num += 1
19+
m1 += 1
20+
m2 -= 1
21+
if m1 == m2:
22+
res[m1][m1] = num
23+
return res

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /