MCPcopy Create free account
hub / github.com/VanjaRo/LeetCode / generateMatrix

Function generateMatrix

tasks/59.go:9–60  ·  view source on GitHub ↗
(n int)

Source from the content-addressed store, hash-verified

7}
8
9func generateMatrix(n int) [][]int {
10 matrix := make([][]int, n)
11 for i := range matrix {
12 matrix[i] = make([]int, n)
13 }
14 x := 0
15 y := 0
16 counter := 1
17 for counter != n*n+1 {
18 for x < n {
19 if matrix[y][x] != 0 {
20 break
21 }
22 matrix[y][x] = counter
23 counter++
24 x++
25 }
26 x--
27 y++
28 for y < n {
29 if matrix[y][x] != 0 {
30 break
31 }
32 matrix[y][x] = counter
33 counter++
34 y++
35 }
36 y--
37 x--
38 for x >= 0 {
39 if matrix[y][x] != 0 {
40 break
41 }
42 matrix[y][x] = counter
43 counter++
44 x--
45 }
46 x++
47 y--
48 for y >= 0 {
49 if matrix[y][x] != 0 {
50 break
51 }
52 matrix[y][x] = counter
53 counter++
54 y--
55 }
56 y++
57 x++
58 }
59 return matrix
60}

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected