Skip to content

Commit 3753740

Browse files
committed
feat: add solutions to lc problem: No.0059
No.0059.Spiral Matrix II
1 parent 2c6b740 commit 3753740

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,31 @@ function generateMatrix(n: number): number[][] {
130130
}
131131
```
132132

133+
```ts
134+
function generateMatrix(n: number): number[][] {
135+
const res = new Array(n).fill(0).map(() => new Array(n).fill(0));
136+
let num = 1;
137+
for (let i = 0; i < Math.floor(n / 2); i++) {
138+
for (let j = i; j < n - i - 1; j++) {
139+
res[i][j] = num++;
140+
}
141+
for (let j = i; j < n - i - 1; j++) {
142+
res[j][n - i - 1] = num++;
143+
}
144+
for (let j = i; j < n - i - 1; j++) {
145+
res[n - i - 1][n - j - 1] = num++;
146+
}
147+
for (let j = i; j < n - i - 1; j++) {
148+
res[n - j - 1][i] = num++;
149+
}
150+
}
151+
if (n % 2 === 1) {
152+
res[n >> 1][n >> 1] = num;
153+
}
154+
return res;
155+
}
156+
```
157+
133158
### **C++**
134159

135160
```cpp
@@ -163,6 +188,40 @@ public:
163188
};
164189
```
165190
191+
### **Rust**
192+
193+
```rust
194+
impl Solution {
195+
pub fn generate_matrix(n: i32) -> Vec<Vec<i32>> {
196+
let n = n as usize;
197+
let mut res = vec![vec![0; n]; n];
198+
let mut num = 1;
199+
for i in 0..n / 2 {
200+
for j in i..n - i - 1 {
201+
res[i][j] = num;
202+
num += 1;
203+
}
204+
for j in i..n - i - 1 {
205+
res[j][n - i - 1] = num;
206+
num += 1;
207+
}
208+
for j in i..n - i - 1 {
209+
res[n - i - 1][n - j - 1] = num;
210+
num += 1;
211+
}
212+
for j in i..n - i - 1 {
213+
res[n - j - 1][i] = num;
214+
num += 1;
215+
}
216+
}
217+
if n % 2 == 1 {
218+
res[n >> 1][n >> 1] = num;
219+
}
220+
res
221+
}
222+
}
223+
```
224+
166225
### **...**
167226

168227
```

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,31 @@ function generateMatrix(n: number): number[][] {
120120
}
121121
```
122122

123+
```ts
124+
function generateMatrix(n: number): number[][] {
125+
const res = new Array(n).fill(0).map(() => new Array(n).fill(0));
126+
let num = 1;
127+
for (let i = 0; i < Math.floor(n / 2); i++) {
128+
for (let j = i; j < n - i - 1; j++) {
129+
res[i][j] = num++;
130+
}
131+
for (let j = i; j < n - i - 1; j++) {
132+
res[j][n - i - 1] = num++;
133+
}
134+
for (let j = i; j < n - i - 1; j++) {
135+
res[n - i - 1][n - j - 1] = num++;
136+
}
137+
for (let j = i; j < n - i - 1; j++) {
138+
res[n - j - 1][i] = num++;
139+
}
140+
}
141+
if (n % 2 === 1) {
142+
res[n >> 1][n >> 1] = num;
143+
}
144+
return res;
145+
}
146+
```
147+
123148
### **C++**
124149

125150
```cpp
@@ -153,6 +178,40 @@ public:
153178
};
154179
```
155180
181+
### **Rust**
182+
183+
```rust
184+
impl Solution {
185+
pub fn generate_matrix(n: i32) -> Vec<Vec<i32>> {
186+
let n = n as usize;
187+
let mut res = vec![vec![0; n]; n];
188+
let mut num = 1;
189+
for i in 0..n / 2 {
190+
for j in i..n - i - 1 {
191+
res[i][j] = num;
192+
num += 1;
193+
}
194+
for j in i..n - i - 1 {
195+
res[j][n - i - 1] = num;
196+
num += 1;
197+
}
198+
for j in i..n - i - 1 {
199+
res[n - i - 1][n - j - 1] = num;
200+
num += 1;
201+
}
202+
for j in i..n - i - 1 {
203+
res[n - j - 1][i] = num;
204+
num += 1;
205+
}
206+
}
207+
if n % 2 == 1 {
208+
res[n >> 1][n >> 1] = num;
209+
}
210+
res
211+
}
212+
}
213+
```
214+
156215
### **...**
157216

158217
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
impl Solution {
2+
pub fn generate_matrix(n: i32) -> Vec<Vec<i32>> {
3+
let n = n as usize;
4+
let mut res = vec![vec![0; n]; n];
5+
let mut num = 1;
6+
for i in 0..n / 2 {
7+
for j in i..n - i - 1 {
8+
res[i][j] = num;
9+
num += 1;
10+
}
11+
for j in i..n - i - 1 {
12+
res[j][n - i - 1] = num;
13+
num += 1;
14+
}
15+
for j in i..n - i - 1 {
16+
res[n - i - 1][n - j - 1] = num;
17+
num += 1;
18+
}
19+
for j in i..n - i - 1 {
20+
res[n - j - 1][i] = num;
21+
num += 1;
22+
}
23+
}
24+
if n % 2 == 1 {
25+
res[n >> 1][n >> 1] = num;
26+
}
27+
res
28+
}
29+
}

0 commit comments

Comments
 (0)