Spiral Matrix II Solutions in Go
Number 59
Difficulty Medium
Acceptance 54.0%
Link LeetCode
Other languages C++
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodefunc generateMatrix(n int) [][]int {if n == 0 {return [][]int{}}if n == 1 {return [][]int{{1}}}res, visit, round, x, y, spDir := make([][]int, n), make([][]int, n), 0, 0, 0, [][]int{{0, 1}, // 朝右{1, 0}, // 朝下{0, -1}, // 朝左{-1, 0}, // 朝上}for i := 0; i < n; i++ {visit[i] = make([]int, n)res[i] = make([]int, n)}visit[x][y] = 1res[x][y] = 1for i := 0; i < n*n; i++ {x += spDir[round%4][0]y += spDir[round%4][1]if (x == 0 && y == n-1) || (x == n-1 && y == n-1) || (y == 0 && x == n-1) {round++}if x > n-1 || y > n-1 || x < 0 || y < 0 {return res}if visit[x][y] == 0 {visit[x][y] = 1res[x][y] = i + 2}switch round % 4 {case 0:if y+1 <= n-1 && visit[x][y+1] == 1 {round++continue}case 1:if x+1 <= n-1 && visit[x+1][y] == 1 {round++continue}case 2:if y-1 >= 0 && visit[x][y-1] == 1 {round++continue}case 3:if x-1 >= 0 && visit[x-1][y] == 1 {round++continue}}}return res}