Leetcode之回溯法专题-39. 组合总数(Combination Sum)

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的数字可以无限制重复被选取。

说明:

  • 所有数字(包括 target)都是正整数。
  • 解集不能包含重复的组合。

示例 1:

输入: candidates = [2,3,6,7], target = 7,
所求解集为:
[
  [7],
  [2,2,3]
]

示例 2:

输入: candidates = [2,3,5], target = 8,
所求解集为:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

分析:给定一个目标值,给定N个数字,要从N个数字里取数字(可以重复选取)求这些数字的和等于目标值的所有情况。

用回溯法很好解决这个问题,首先从0-(n-1)中选取第1个数,第一个数取1-k次,以此类推。

AC代码如下:
class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {

        dfs(candidates,target,0,target,new ArrayList<Integer>());
        return ans;
    }

    public void dfs(int[] candidates,int target,int index,int balance,ArrayList<Integer> list){
        if(balance<0){
            return;
        }
        if(balance==0){

            ans.add(new ArrayList<Integer>(list));
            return;
        }

        if(index>=candidates.length){
            return;
        }

        int num = candidates[index];
        int n = balance/num +1;

        for(int i=0;i<=n;i++){

            for(int j=0;j<i;j++){
                list.add(num);
            }
            int count = num*i;
            dfs(candidates,target,index+1,balance-count,list);
            for(int j=0;j<i;j++){
                list.remove(list.size()-1);
            }
        }

    }
}

原文地址:https://www.cnblogs.com/qinyuguan/p/11318109.html

时间: 2024-08-29 12:12:48

Leetcode之回溯法专题-39. 组合总数(Combination Sum)的相关文章

Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:["ad", "ae", "af", "bd", "be", "bf&qu

[Leetcode] Backtracking回溯法解题思路

碎碎念: 最近终于开始刷middle的题了,对于我这个小渣渣确实有点难度,经常一两个小时写出一道题来.在开始写的几道题中,发现大神在discuss中用到回溯法(Backtracking)的概率明显增大.感觉如果要顺利的把题刷下去,必须先要把做的几道题题总结一下. 先放上参考的web: https://segmentfault.com/a/1190000006121957 http://summerisgreen.com/blog/2017-07-07-2017-07-07-算法技巧-backtr

回溯法专题

回溯法专题 回溯法(Backtracking)指的是在每个状态的固化,比如f(1)是一个状态,f(2)是另一个状态.从f(1)到f(2),状态改变,各种依赖状态的数据也改变了,那么从f(2)到f(1)的时候,又回到了f(1)的状态了.回溯常常配合深度优先执行,在往深度时候,数据产生变化,然后在递归回来的时候,又回到了之前的状态. 46. Permutations 拿这道全排列来讲,它按照正常顺序列出了全部的排列结果,也可以叫做字典序,主要是输入是按从小到大的话,输出就是字典序. 按照递归的方式来

【LeetCode】回溯法 backtracking(共39题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses (2019年2月13日) 给了一个N,生成N对括号的所有情况的字符串. n = 3 [ "((()))", "(()())", "(

Leetcode 39 40 216 Combination Sum I II III

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi

[Swift]LeetCode39. 组合总和 | Combination Sum

Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. The same repeated number may be chosen from candidates unlimited n

从Leetcode的Combination Sum系列谈起回溯法

在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2.3都可以通过回溯法来解决,其实4也可以,不过由于递归地比较深,采用回溯法会出现TLE.因此本文只讨论前三题. 什么是回溯法?回溯法是一种选优搜索法,按选优条件向前搜索以达到目标.当探索到某一步时,发现原先的选择并不优或达不到目标,就退回异步重新选择.回溯法是深度优先搜索的一种,但回溯法在求解过程不

[Leetcode 40]组合数和II Combination Sum II

[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: A

LeetCode:组合总数II【40】

LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. 说明: 所有数字(包括目标数)都是正整数. 解集不能包含重复的组合. 示例 1: 输入: candidates = [10,1,2,7,6,1,5], target = 8, 所求解集为: [ [1, 7], [1, 2, 5], [2, 6],