douhuan6065 2017-03-03 06:57
浏览 12
已采纳

切片值为什么会变化?

The value of a slice that be appended to a two-dimension slice, is changed later。 Why does it happen?

The full code is below:

import "sort"
import "fmt"

func combinationSum(candidates []int, target int) [][]int {
    sort.Ints(candidates)
    var ret [][]int
    var curlist []int
    combinationSumRecursive(candidates, target, 0, curlist, &ret)
    fmt.Println("ret", ret)
    return ret
}

func combinationSumRecursive(candidates []int, target int, curCandidateIdx int, curlist []int, ret *[][]int) {
    if target == 0 {
        fmt.Println("put", curlist)
        *ret = append(*ret, curlist)
        return
    }
    for i:=curCandidateIdx;i<len(candidates);i++{
        if candidates[i] > target {
            break
        }
        combinationSumRecursive(candidates, target - candidates[i], i, append(curlist, candidates[i]), ret)
    }
}

INPUT is :

[7,3,2]
18

OUTPUT is below:

put [2 2 2 2 2 2 2 2 2]
put [2 2 2 2 2 2 3 3]
put [2 2 2 2 3 7]
put [2 2 2 3 3 3 3]
put [2 2 7 7]
put [2 3 3 3 7]
put [3 3 3 3 3 3]
ret [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 7 3 3] [2 2 2 2 3 7] [2 2 2 3 3 3 3] [2 2 7 7] [2 3 3 3 7] [3 3 3 3 3 3]]

The second slice in ret is changed. [2 2 2 2 2 2 3 3] was expected.

Thanks to any help.

  • 写回答

2条回答 默认 最新

  • douyangquan2474 2017-03-03 09:26
    关注

    This is caused by the change of the slice curlist in the append on line combinationSumRecursive(candidates, target - candidates[i], i, append(curlist, candidates[i]), ret).

    A solution is to reassign curlist to the appended result before passing it to the next recursive level.

    package main
    
    import "sort"
    import "fmt"
    
    func combinationSum(candidates []int, target int) [][]int {
        sort.Ints(candidates)
        var ret [][]int
        var curlist []int
        combinationSumRecursive(candidates, target, 0, curlist, &ret)
        fmt.Println("ret", ret)
        return ret
    }
    
    func combinationSumRecursive(candidates []int, target int, curCandidateIdx int, curlist []int, ret *[][]int) {
        if target == 0 {
            fmt.Println("put", curlist)
            *ret = append(*ret, curlist)
            return
        }
        for i := curCandidateIdx; i < len(candidates); i++ {
            if candidates[i] > target {
                break
            }
            curlist = append(curlist, candidates[i])
            combinationSumRecursive(candidates, target-candidates[i], i, curlist, ret)
        }
    }
    
    func main() {
        combinationSum([]int{7, 3, 2}, 18)
    }
    

    This yields:

    put [2 2 2 2 2 2 2 2 2]
    put [2 2 2 2 2 2 3 3]
    put [2 2 2 2 3 7]
    put [2 2 2 3 3 3 3]
    put [2 2 7 7]
    put [2 3 3 3 7]
    put [3 3 3 3 3 3]
    ret [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 2 3 3] [2 2 2 2 3 7] [2 2 2 3 3 3 3] [2 2 7 7] [2 3 3 3 7] [3 3 3 3 3 3]]
    

    [EDIT] First read this. That explains that although you see the addresses of the slice headers change the backing array can still be the same after an append.

    Then this is an example of what happens in your case. The problem is in your case is it hard to see because of the recursive calls and the re positioning of the current index into curlist.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址