leetcode:Combination Sum(I,II)

本文详细解析了组合求和问题的两种变体及其对应的回溯算法实现,包括允许候选数无限次重复使用的组合求和问题及限制每个候选数仅使用一次的情况。通过具体的示例阐述了解决方案的设计思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题:
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 (including target) will be positive integers.
The solution set must not contain duplicate combinations.

For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:

[ [7], [2, 2, 3] ]

升级:
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:

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

For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
A solution set is:

[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ]

回溯法:

Combination Sum

public class Solution {

        public static List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        Arrays.sort(candidates);
        backtrace(result,new ArrayList<Integer>(),candidates,target,0);
        return result;
    }
    private static void backtrace(List<List<Integer>> list, ArrayList<Integer> tempList, int [] nums, int remain, int start){
        if(remain < 0) return;
        else if(remain == 0) list.add(new ArrayList<>(tempList));
        else{ 
            for(int i = start; i < nums.length; i++){
                tempList.add(nums[i]);
                backtrace(list, tempList, nums, remain - nums[i], i); //从i开始,集合可以含有重复元素
                tempList.remove(tempList.size() - 1);//最后一个元素放入元素使得remain<0,故要排除最后一个元素,按原路回退一步
            }
        }
    }
}

Combination Sum(II)

public class Solution {
    public static List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        Arrays.sort(candidates);
        backtrack(result,new ArrayList<Integer>(),candidates,target,0);
        return result; 
    }
    private static void backtrack(List<List<Integer>> result,ArrayList<Integer> tempList, int[] candidates, int remain, int idx) {
        if(remain<0) return ;
        else if(remain==0) result.add(new ArrayList<>(tempList));
        else{
            for (int i = idx; i < candidates.length; i++) {
                if(i > idx && candidates[i] == candidates[i-1]) continue;//跳过重复元素
                tempList.add(candidates[i]);
                backtrack(result, tempList, candidates, remain-candidates[i], i+1);不能从自身元素开始搜索
                tempList.remove(tempList.size()-1);
            }
        }
    } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值