跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • golang 求助大佬
未分類
7 11 月 2020

golang 求助大佬

golang 求助大佬

資深大佬 : an93 5

import “fmt”

func main() {     input := []int{2,3, 5}     target := 8     fmt.Println(combinationSum(input, target))  }  func combinationSum(candidates []int, target int) [][]int {    var res [][]int     var backtrack func(int, int, []int)    backtrack = func(idx int, sum int, nums []int) {         if sum == target {            res = append(res, nums)        }        if sum >= target {            return        }        for i := idx; i < len(candidates); i++ {            backtrack(i, sum + candidates[i], append(nums,candidates[i]))        }    }    backtrack(0, 0, []int{})    return res } 

这段代码结果有问题, 而且问题处在 res = append(res, nums) 这一行, 如果我把 nums copy 一个再 append 就不会有问题了,听说这里大神比较多,求大神们指教一下

大佬有話說 (3)

  • 主 資深大佬 : an93

    https://play.studygolang.com/p/Smu8A5nw-rV

  • 資深大佬 : luguhu

    slice 是指针传递

  • 資深大佬 : zhouenxian

    函数传参数确实是值传递,但 slice 传递的值中包含了内容的地址,所以还是把地址传了进去。
    如下为切片类型的内部定义。
    type _slice struct {
    elements unsafe.Pointer // 引用着底层存储在间接部分上的元素
    len int // 长度
    cap int // 容量
    }
    引用:go 101,https://gfw.go101.org/article/container.html

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具