dongyan3616 2017-08-25 11:00
浏览 24
已采纳

递归追加到切片不起作用

I'm trying to learn Go but I can't figure it out why this code at the end of the recursion call stack returns an empty slice, any help? Also tmp doesn't even seem to register in the debugger.

func main() {
    input := [3]int{4, 6, 7}
    // expected [[6,7],[4,6,7],[4,6],[4,7]]
    fmt.Println(findSubsequences(input))
}

func findSubsequences(nums [3]int) [][]int {
    res := [][]int{}
    list := []int{}
    findSubsequence(res, list, nums, 0)
    return res
}

func findSubsequence(res [][]int, list []int, nums [3]int, id int) [][]int {
    if len(list) > 1 {
        tmp := make([]int, len(list))
        copy(tmp, list)
        res = append(res, tmp)
    }
    var unique []int
    for i := id; i < len(nums); i++ {
        if id > 0 && nums[i] < nums[id-1] {
            continue // skip non-increase
        }
        if contains(unique, nums[i]) {
            continue // skip duplicate
        }
        unique = append(unique, nums[i])
        list = append(list, nums[i])
        findSubsequence(res, list, nums, id+1)
        list = list[:len(list)-1]
    }
    return res
}

func contains(s []int, e int) bool {
    for _, a := range s {
        if a == e {
            return true
        }
    }
    return false
}
  • 写回答

2条回答 默认 最新

  • duanhoupeng6642 2017-08-26 22:07
    关注

    This is the solution to get your code to append the slice. In GO, if you are recursively passing a slice, you must pass it by reference. So this solves the problem that you are experiencing where your code will return empty slice. But your algorithm seems incorrect for the result that you are expecting.

    func main() {
        input := [3]int{4, 6, 7}
        // expected [[6,7],[4,6,7],[4,6],[4,7]]
        fmt.Println(findSubsequences(input))
    }
    
    func findSubsequences(nums [3]int) [][]int {
        res := [][]int{}
        list := []int{}
        fmt.Print(nums)
        findSubsequence(&res, list, nums, 0)
        return res
    }
    
    func findSubsequence(res *[][]int, list []int, nums [3]int, id int) [][]int {
        var tmp []int
        if len(list) > 1 {
            tmp = make([]int, len(list))
            copy(tmp, list)
        fmt.Println(tmp)
            *res = append(*res, tmp)
        }
        var unique []int
        for i := id; i < len(nums); i++ {
            if id > 0 && nums[i] < nums[id-1] {
                continue // skip non-increase
            }
            if contains(unique, nums[i]) {
                continue // skip duplicate
            }
            unique = append(unique, nums[i])
            list = append(list, nums[i])
            findSubsequence(res, list, nums, id+1)
        list = list[:len(list)-1]
    
        }
        return *res
    }
    
    func contains(s []int, e int) bool {
        for _, a := range s {
            if a == e || a >e {
                return true
            }
        }
        return false
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化