题目:
题解:
func generateMatrix(n int) [][]int {matrix := make([][]int, n)for i := range matrix {matrix[i] = make([]int, n)}num := 1left, right, top, bottom := 0, n-1, 0, n-1for left <= right && top <= bottom {for column := left; column <= right; column++ {matrix[top][column] = numnum++}for row := top + 1; row <= bottom; row++ {matrix[row][right] = numnum++}if left < right && top < bottom {for column := right - 1; column > left; column-- {matrix[bottom][column] = numnum++}for row := bottom; row > top; row-- {matrix[row][left] = numnum++}}left++right--top++bottom--}return matrix
}