Leetcode Combination sum I, II

本文详细解析了两种组合求和算法:CombinationsumI 和 CombinationsumII。这两种算法分别适用于候选数可以无限次重复使用的情况及每个候选数只能使用一次的情况。通过具体的示例代码展示了如何找出所有可能的组合,使候选数之和等于目标数。

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

Combination sum I

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.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
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]

[code]

public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> list=new ArrayList<List<Integer>>();
        ArrayList<Integer> path=new ArrayList<Integer>();
        if(candidates == null || candidates.length==0)return list;
        Arrays.sort(candidates);
        dfs(candidates, 0, target, path, list);
        return list;
    }

    void dfs(int[]candidates, int index, int target, ArrayList<Integer> path, List<List<Integer>> list)
    {
        if(target==0)
        {
            list.add(new ArrayList<Integer>(path));
            return;
        }
        if(index==candidates.length)return;
        int sum=0;
        while(sum<=target)
        {
            dfs(candidates, index+1, target-sum, path, list);
            path.add(candidates[index]);
            sum+=candidates[index];
        }
        for(int i=0;i<sum/candidates[index];i++)path.remove(path.size()-1);                                                                        
    }
}

Combination sum II

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.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
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]

[code]

public class Solution {
    public List<List<Integer>> combinationSum2(int[] num, int target) {
        List<List<Integer>> list=new ArrayList<List<Integer>>();
        ArrayList<Integer> path =new ArrayList<Integer>();
        if(num==null || num.length==0)return list;
        Arrays.sort(num);
        dfs(num, 0, target, path, list);
        return list;
    }
    void dfs(int[]num, int index, int target, ArrayList<Integer> path, List<List<Integer>> list)
    {
        if(target==0)
        {
            list.add(new ArrayList<Integer>(path));
            return;
        }
        if(index==num.length || num[index]>target)return;
        int i=index+1;
        while(i<num.length && num[i]==num[index])
        {
            i++;
        }
        int sum=0;
        for(int j=0;j<=i-index;j++)
        {
            //if(sum>target)break;
            dfs(num, i, target-sum, path, list);
            path.add(num[index]);
            sum+=num[index];
        }
        for(int j=0;j<sum/num[index];j++)
        {
            path.remove(path.size()-1);
        }
    }
}

[Thoughts]
这种有duplicate的dfs 避免重复, for:i=0->n个 num[i]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值