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 76ff30a

Browse files
authored
feat: add go solution to lc problem: No.0059 (#839)
No.0059.Spiral Matrix II
1 parent 4e2469e commit 76ff30a

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,42 @@ impl Solution {
222222
}
223223
```
224224

225+
### **Go**
226+
227+
```go
228+
func generateMatrix(n int) [][]int {
229+
res := make([][]int, n)
230+
for i := range res {
231+
res[i] = make([]int, n)
232+
}
233+
elem := 1
234+
top, bottom, left, right := 0, n-1, 0, n-1
235+
for elem <= n*n {
236+
for i := left; i <= right; i++ {
237+
res[top][i], elem = elem, elem+1
238+
}
239+
top++
240+
for i := top; i <= bottom; i++ {
241+
res[i][right], elem = elem, elem+1
242+
}
243+
right--
244+
if top <= bottom {
245+
for i := right; i >= left; i-- {
246+
res[bottom][i], elem = elem, elem+1
247+
}
248+
bottom--
249+
}
250+
if left <= right {
251+
for i := bottom; i >= top; i-- {
252+
res[i][left], elem = elem, elem+1
253+
}
254+
left++
255+
}
256+
}
257+
return res
258+
}
259+
```
260+
225261
### **...**
226262

227263
```

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,42 @@ impl Solution {
212212
}
213213
```
214214

215+
### **Go**
216+
217+
```go
218+
func generateMatrix(n int) [][]int {
219+
res := make([][]int, n)
220+
for i := range res {
221+
res[i] = make([]int, n)
222+
}
223+
elem := 1
224+
top, bottom, left, right := 0, n-1, 0, n-1
225+
for elem <= n*n {
226+
for i := left; i <= right; i++ {
227+
res[top][i], elem = elem, elem+1
228+
}
229+
top++
230+
for i := top; i <= bottom; i++ {
231+
res[i][right], elem = elem, elem+1
232+
}
233+
right--
234+
if top <= bottom {
235+
for i := right; i >= left; i-- {
236+
res[bottom][i], elem = elem, elem+1
237+
}
238+
bottom--
239+
}
240+
if left <= right {
241+
for i := bottom; i >= top; i-- {
242+
res[i][left], elem = elem, elem+1
243+
}
244+
left++
245+
}
246+
}
247+
return res
248+
}
249+
```
250+
215251
### **...**
216252

217253
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
func generateMatrix(n int) [][]int {
2+
res := make([][]int, n)
3+
for i := range res {
4+
res[i] = make([]int, n)
5+
}
6+
elem := 1
7+
top, bottom, left, right := 0, n-1, 0, n-1
8+
for elem <= n*n {
9+
for i := left; i <= right; i++ {
10+
res[top][i], elem = elem, elem+1
11+
}
12+
top++
13+
for i := top; i <= bottom; i++ {
14+
res[i][right], elem = elem, elem+1
15+
}
16+
right--
17+
if top <= bottom {
18+
for i := right; i >= left; i-- {
19+
res[bottom][i], elem = elem, elem+1
20+
}
21+
bottom--
22+
}
23+
if left <= right {
24+
for i := bottom; i >= top; i-- {
25+
res[i][left], elem = elem, elem+1
26+
}
27+
left++
28+
}
29+
}
30+
return res
31+
}

0 commit comments

Comments
(0)

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