LeetCode-39-Combination Sum-M(组合和)

本文详细解析了如何使用递归回溯思想解决组合求和问题,即从无重复的候选数集中找到所有可能的组合,使得这些组合的元素之和等于目标数。文章通过实例演示了算法的工作流程,并介绍了剪枝策略以提高效率。

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 number of times.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

Example 1:
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]

Example 2:
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]

和46、47题有一点类似。采用递归回溯的思想,每一次把candidates的第一个数字X加入temp,然后再次调用算法,target替换为target-X,调用结束后去掉temp顶端的数字。

举例,对于candidates = [2,3,5], target = 8。首先加入2,然后对candidates = [2,3,5], target = 6调用算法,此时的标记位i依然在2的位置。

最后,加入剪枝的策略,如果已经放入temp的数字加在一起超过了target,那么直接返回并去掉temp顶端的数字。

再举例,对于candidates = [2,3,6,7], target = 7。首先加入2。然后对candidates = [2,3,5], target =5调用算法,再加入2,再加入2,再加入2,此时对candidates = [2,3,5], target = -1调用算法(标记位i依然在2的位置),发现target<0,直接返回并去掉顶端的2。此时相当于对candidates = [2,3,5], target = 1走完了第一次循环,标记位移动到3,依然不能满足,标记位移动到6和7还是不能满足,此时走完所有循环,之后返回,再去掉顶端的2。相当于对candidates = [2,3,5], target = 3走完了第一次循环,标记位移动到3,满足,返回一组成功的结果。以此类推。

    List<List<Integer>> res=new ArrayList<>();
    
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<Integer> temp=new ArrayList<>();
        comb(candidates,target,temp,0);
        return res;
    }
    
    public void comb(int[] candidates,int target,List<Integer> temp,int start){
        if(target<0) //剪枝
            return;
        else if(target==0)
            res.add(new ArrayList<>(temp));
        else{
            for(int i=start;i<candidates.length;i++){
                temp.add(candidates[i]);
                comb(candidates,target-candidates[i],temp,i);
                temp.remove(temp.size()-1);
            }
        }
    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值