美文网首页leetcode
77. Combinations.go

77. Combinations.go

作者: AnakinSun | 来源:发表于2019-03-23 13:32 被阅读0次

    回溯算法

    func combine(n int, k int) [][]int {
        var res [][]int
        helper(&res, []int{}, 1, n, k)
        return res
    }
    
    func helper(res *[][]int, coms []int, start int, n int, k int) {
        if k == 0 {
            tmp := []int{}
            tmp = append(tmp, coms...)
            *res = append(*res, tmp)
            return
        }
        for i := start; i <= n; i++ {
            coms = append(coms, i)
            helper(res, coms, i+1, n, k-1)
            coms = coms[:len(coms)-1]
        }
    }
    

    相关文章

      网友评论

        本文标题:77. Combinations.go

        本文链接:https://www.haomeiwen.com/subject/qlcsvqtx.html