golang leetcode59 螺旋生成 matrix - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cmyun
V2EX    LeetCode

golang leetcode59 螺旋生成 matrix

  •  
  •   cmyun 2019-11-09 16:42:10 +08:00 12827 次点击
    这是一个创建于 2229 天前的主题,其中的信息可能已经有所发展或是发生改变。

    leetcode 59.螺旋的方式生成一个 n*n 的 matrix Example:

    Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]

    func generateMatrix(n int) [][]int { matrx:=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 ] ]
    debug 过程中,发现在运行递归函数的时候报错,如下

    报错: 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 

    大佬帮忙看看这是为什么报错???

    1 条回复    2019-11-09 17:02:51 +08:00
    cmyun
        1
    cmyun  
    OP
       2019-11-09 17:02:51 +08:00
    个位路过的大佬,拜托帮忙指正一下
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2955 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 13:47 PVG 21:47 LAX 05:47 JFK 08:47
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86