leetcode 59.螺旋的方式生成一个n*n的matrix
Example:
Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
```golang
func generateMatrix(n int) [][]int {
matrix:=make([][]int,n)
for i:=0;i<n;i++{
matrix[i]=make([]int,n)
}
helper(matrix,n,1,0,n-1,0,n-1)
return matrix
}
func helper(matrix [][]int,n,index,row0,row1,column0,column1 int){
if index>n{
return
}
//上边
if row0 <= row1 && column0 <= column1{
for i:=column0;i<=column1;i++{
matrix[row0][i]=index
index++
}
}
//右边
if column0 <= column1 && row0<row1{
for i:=row0+1;i<=row1;i++{
matrix[i][column1]=index
index++
}
}
//下边
if row0 < row1 && column0 < column1{
for i:=column1-1;i>=column0;i--{
matrix[row1][i]=index
index++
}
}
//左边
if row0<row1-1 && column0<column1{
for i:=row1-1;i>row0;i--{
matrix[i][column0]=index
index++
}
}
//递归内层
helper(matrix,n,index,row0+1,row1-1,column0+1,column1-1)
}
```
我的运行结果是 [ [ 1, 2, 3 ], [ 8, 0, 4 ], [ 7, 6, 5 ] ]</br>
debug过程中,发现在运行递归函数的时候报错,如下
```text
报错:
Exception 0xc000001d 0x0 0x0 0x4bd2ea
PC=0x4bd2ea
leetcode/leetcode59.helper(0xc0000a4000, 0x3, 0x3, 0x3, 0x9, 0x1, 0x1, 0x1, 0x1)
D:/*/leetcode/leetcode59/leetcode59.go:29 +0x47a
leetcode/leetcode59.helper(0xc0000a4000, 0x3, 0x3, 0x3, 0x9, 0x0, 0x2, 0x0, 0x2)
D:/*/leetcode/leetcode59/leetcode59.go:63 +0x41b
leetcode/leetcode59.generateMatrix(0x3, 0x0, 0x0, 0x0)
D:/*/leetcode/leetcode59/leetcode59.go:25 +0x1c9
leetcode/leetcode59.Run()
D:/*/leetcode/leetcode59/leetcode59.go:67 +0x46
main.main()
D:/*/leetcode/main.go:6 +0x27
rax 0x1
rbx 0x8
rcx 0xc000038000
rdi 0xc000078140
rsi 0x3
rbp 0xc00009bd78
rsp 0xc00009bd00
r8 0x100
r9 0x7
r10 0x39
r11 0x0
r12 0x2030000
r13 0x49249249249249
r14 0x18
r15 0x7
rip 0x4bd2ea
rflags 0x10203
cs 0x33
fs 0x53
gs 0x2b
```
大佬帮忙看看这是哪里错了???
有疑问加站长微信联系(非本文作者))