LeetCode 39. 组合总和

简介: LeetCode 39. 组合总和

 39. 组合总和

   

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

示例 1:

输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。7 也是一个候选, 7 = 7 。仅有这两种组合。

示例 2:

输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]

示例 3:

输入: candidates = [2], target = 1
输出: []

提示:

    • 1 <= candidates.length <= 30
    • 2 <= candidates[i] <= 40
    • candidates 的所有元素 互不相同
    • 1 <= target <= 40


    思路

    参考 B站视频题解,从下标i=0开始不断尝试添加新元素,通过回溯搜索最终找到满足要求的方案。


    时间复杂度:见 LeetCode官方题解

    空间复杂度

    O(target)。除答案数组外,空间复杂度取决于递归的栈深度,在最差情况下需要递归 O(target) 层。

    // 版本1 累加sum,用于与target做比较// var res [][]int// func combinationSum(candidates []int, target int) [][]int {//     dfs(candidates, target, []int{}, 0)//     return res// }// func dfs(candidates []int, target int, arr []int, sum int) {//     if sum > target {//         return//     }//     if sum == target {//         tmpArr := make([]int, len(arr))//         copy(tmpArr, arr)//         res = append(res, tmpArr)//         return//     }//     // sum < target//     for i := 0; i < len(candidates); i++ {//         arr = append(arr, candidates[i])// 追加//         dfs(candidates, target, arr, sum + candidates[i])//         arr = arr[:len(arr)-1]          // 回溯//     }// }// 版本2:累加sum,用于与target做比较// 版本1会把不同顺序的选取方式作为不同的方案,所以此时需要去掉重复的情况,dfs()中另加index参数// 思路:从下标i=0开始不断尝试添加新元素,通过回溯搜索最终找到满足要求的方案。// 参考 B站视频:https://www.bilibili.com/video/BV1h14y1A7yR/?spm_id_from=333.337.search-card.all.click&vd_source=2c268e25ffa1022b703ae0349e3659e4// var res [][]int// func combinationSum(candidates []int, target int) [][]int {//     res = make([][]int, 0) // 一定要加这一行,初始化res,否则不同用例在跑的时候 res会被覆盖导致 res值为上次的值//     dfs(candidates, target, []int{}, 0, 0)//     return res// }// func dfs(candidates []int, target int, arr []int, sum, index int) {//     if sum > target {//         return//     }//     if sum == target {//         tmpArr := make([]int, len(arr))//         copy(tmpArr, arr)//         res = append(res, tmpArr)//         return//     }//     // sum < target//     // 避免每次遍历candidates时从i=0开始,导致结果重复,比如2,2,3和2,3,2 !!!//     for i := index; i < len(candidates); i++ {//         arr = append(arr, candidates[i])// 追加//         dfs(candidates, target, arr, sum + candidates[i], i)//         arr = arr[:len(arr)-1]          // 回溯//     }// }// 版本3:去除sum参数,仅利用target每次递减即可varres [][]intfunccombinationSum(candidates []int, targetint) [][]int {
    res=make([][]int, 0)
    dfs(candidates, target, 0, []int{})
    returnres}
    funcdfs(candidates []int, target, indexint, arr []int) {
    iftarget==0 {
    tmp :=make([]int, len(arr))
    copy(tmp, arr)
    res=append(res, tmp)
    return    } elseiftarget<0 {
    return    }
    fori :=index; i<len(candidates); i++ {
    arr=append(arr, candidates[i])
    // 因为同一个 数字可以 无限制重复被选取,所以第三个参数传入i而不是i+1dfs(candidates, target-candidates[i], i, arr)
    arr=arr[:len(arr) -1]
        }
    }
    // 版本4 动态规划的思想// 参考 B站视频:https://www.bilibili.com/video/BV1h14y1A7yR/?spm_id_from=333.337.search-card.all.click&vd_source=2c268e25ffa1022b703ae0349e3659e4

    image.gif


    目录
    相关文章
    |
    9月前
    |
    人工智能 BI 索引
    【Leetcode -598.范围求和Ⅱ -599.两个列表的最小索引总和】
    【Leetcode -598.范围求和Ⅱ -599.两个列表的最小索引总和】
    30 0
    |
    2月前
    |
    存储 算法
    算法题解-组合总和3
    算法题解-组合总和3
    |
    2月前
    |
    Java
    leetcode-377:组合总和 Ⅳ
    leetcode-377:组合总和 Ⅳ
    22 0
    |
    2月前
    |
    Java
    leetcode-40:组合总和 II
    leetcode-40:组合总和 II
    29 0
    |
    2月前
    |
    Java 索引
    leetcode-39:组合总和
    leetcode-39:组合总和
    23 0
    |
    2月前
    |
    Java
    leetcode-216:组合总和 III
    leetcode-216:组合总和 III
    24 0
    |
    9月前
    代码随想录Day22 LeetCode T39 组合总和 T40 组合总和II T131 分割回文串
    代码随想录Day22 LeetCode T39 组合总和 T40 组合总和II T131 分割回文串
    24 0
    |
    机器学习/深度学习 算法 安全
    LeetCode - #40 组合总和 II
    不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。如果大家有建议和意见欢迎在文末留言,我们会尽力满足大家的需求。
    leetcode:39.组合总和
    给定一个无重复元素的数组 candidates和一个目标数 target,找出 candidates中所有可以使数字和为 target的组合。
    50 0
    leetcode:40.组合总和 II
    给定一个数组 candidates和一个目标数 target,找出 candidates中所有可以使数字和为 target的组合。
    40 0