Leetcode40 Combination Sum II

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]

Solution1

  • 依然是典型的回溯解法,和leetcode39稍有些不同,也就是数组中的每个数只能用一次。
public class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        Arrays.sort(candidates);
        help(candidates,0,0,target,new ArrayList<Integer>(),result);
        return result;
    }
    public void help(int[] candidates, int index, int sum, int target, List<Integer> item, List<List<Integer>> result){
        if(sum==target){
            result.add(item);
            return;
        }
        if(index>=candidates.length||sum>target) return;
        for(int i=index;i<candidates.length;i++){
            if(i>index&&candidates[i]==candidates[i-1]) continue;//注意是大于index
            List<Integer> temp = new ArrayList<Integer>(item);
            temp.add(candidates[i]);
            help(candidates,i+1,sum+candidates[i],target,temp,result);
        }        
    }
}

Solution2

  • 迭代的解法,用一个stack来记录数值下标。
public class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        Arrays.sort(candidates);
        int sum = 0;
        List<Integer> item = new ArrayList<Integer>();
        Stack<Integer> stack = new Stack<Integer>();

        for(int i=0;;){
            while(i<candidates.length&&sum+candidates[i]<target){
                sum += candidates[i];
                stack.push(i);
                item.add(candidates[i++]);
            }
            if(i<candidates.length&&sum+candidates[i]==target){
                List<Integer> temp = new ArrayList<Integer>(item);
                temp.add(candidates[i]);
                result.add(temp);
            }
            if(stack.empty()) return result;
            item.remove(item.size()-1);
            i = stack.pop();    
            sum -= candidates[i++];
            while(i<candidates.length&&candidates[i-1]==candidates[i]) i++;//去掉重复的数
        }
    }
}

Solution3

  • 动态规划的解法,其本质和限定了数量的找零钱问题一样,这里主要注意的问题是重复的现象。这种方法并不推荐,太容易出错,必须对每一步都要非常清楚。
public class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(candidates.length==0) return result;
        Arrays.sort(candidates);
        result.add(new ArrayList<Integer>());
        HashMap<Integer,List<List<Integer>>> dp = new HashMap<Integer,List<List<Integer>>>();
        dp.put(0,result);
        for(int sum=candidates[0];sum<=target;sum++){
            result = new ArrayList<List<Integer>>();
            for(int i=0;i<candidates.length;i++){
                if(!dp.containsKey(sum-candidates[i])) continue;
                for(List<Integer> item:dp.get(sum-candidates[i])){
                    int j;
                    for(j=i;item.size()>0&&j<candidates.length&&j<=item.get(item.size()-1);) j++;
                    if(j>=candidates.length||candidates[j]!=candidates[i]) break;
                    List<Integer> temp = new ArrayList<Integer>(item);
                    temp.add(j);
                    result.add(temp);
                }
                while(i+1<candidates.length&&candidates[i+1]==candidates[i]) i++;//跳过重复的数
            }
            if(!result.isEmpty()) dp.put(sum,result);
        }
        result = new ArrayList<List<Integer>>();
        if(dp.get(target)==null) return result;
        for(List<Integer> item:dp.get(target)){
            List<Integer> temp = new ArrayList<Integer>();
            for(int num:item) temp.add(candidates[num]);
            if(!temp.isEmpty()) result.add(temp);
        }
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值