From 2cc863190c08f0df0c2b52442614b47a255d109e Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 25 May 2021 00:07:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200059.=E8=9E=BA=E6=97=8B?= =?UTF-8?q?=E7=9F=A9=E9=98=B5II=20Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0059.螺旋矩阵II.md | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index e92b5714..6d8ec99c 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -267,6 +267,45 @@ var generateMatrix = function(n) { }; ``` +Go: + +```go +func generateMatrix(n int) [][]int { + top, bottom := 0, n-1 + left, right := 0, n-1 + num := 1 + tar := n * n + matrix := make([][]int, n) + for i := 0; i < n; i++ { + matrix[i] = make([]int, n) + } + for num <= tar { + for i := left; i <= right; i++ { + matrix[top][i] = num + num++ + } + top++ + for i := top; i <= bottom; i++ { + matrix[i][right] = num + num++ + } + right-- + for i := right; i >= left; i-- { + matrix[bottom][i] = num + num++ + } + bottom-- + for i := bottom; i >= top; i-- { + matrix[i][left] = num + num++ + } + left++ + } + return matrix +} +``` + + -----------------------