leetcode 40. 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.
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]
]

图示:这里写图片描述
可以看到和上一道题目的不同就是拿第i个候选数字进行尝试的时候,子问题就要把这个候选数字减少一个,
为了保证单个结果中的数字不递减,采用的方法是子问题从当前问题的序号开始搜索,那么这个问题中,2367拿6尝试 子问题中候选变成237 应该从7开始尝试 也是序号为3 所以仍然是从i开始尝试
还有就是子问题结束时应该把所尝试的数字(6)补回来 才能在父问题中尝试7

Stack<Integer> result = new Stack<Integer>();
    List<List<Integer>> results = new ArrayList<List<Integer>>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        List<Integer> listOfCandidates =  new ArrayList<Integer>();
        for(int temp:candidates) listOfCandidates.add(temp);
        backTracking(listOfCandidates, target,0);
        return results;
    }

    public void  backTracking(List<Integer> candidates,int target,int from){
        if(target == 0) {
            List<Integer> copy = new LinkedList<Integer>();
            copy.addAll(result);
            results.add(copy);
        }
        else{
            if(candidates.get(0) > target)
                return ;
            else{
                for(int i = from;i < candidates.size() && candidates.get(0) <= target;i++){
                    result.push(candidates.get(i));
                    backTracking(candidates,target-candidates.remove(i),i);
                    candidates.add(i, result.pop());
                }
            }
        }
    }

改了改就有了第一版:
因为候选集合不在父问题与子问题中要变 所以再用数组就不合适 改成了用LinkedList 然后就是分治递归的这里:
result.push(candidates.get(i));
backTracking(candidates,target-candidates.remove(i),i);
candidates.add(i, result.pop());
非常完美
然后有问题:new int[]{10, 1, 2, 7, 6, 1, 5}, 8的结果是:
116
125
17
125
17
26
看到有重复的
上一道题目候选者没有重复的 所以只要路径注意递增就可以直接出结果
这个题目 1,1,2,5,6,7,10本身1,1是重复的 怎么避免呢?
好吧看了大神的 这个也没有在这里避免 是在出结果的时候去重复了 那我也这样做吧>_<

public class Solution {
    Stack<Integer> result = new Stack<Integer>();
    List<List<Integer>> results = new ArrayList<List<Integer>>();

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        List<Integer> listOfCandidates =  new LinkedList<Integer>();
        for(int temp:candidates) listOfCandidates.add(temp);
        backTracking(listOfCandidates, target,0);
        Set<List<Integer>> setOfResults = new HashSet<List<Integer>>();
        results.forEach(a->setOfResults.add(a));
        results.clear();
        setOfResults.forEach(a->results.add(a));
        return results;
    }

    public void  backTracking(List<Integer> candidates,int target,int from){
        if(target == 0) {
            List<Integer> copy = new ArrayList<Integer>();
            copy.addAll(result);
            results.add(copy);
        }
        else{
            if(candidates.size() == 0 || candidates.get(0) > target)
                return ;
            else{
                for(int i = from;i < candidates.size() && candidates.get(0) <= target;i++){
                    result.push(candidates.get(i));
                    backTracking(candidates,target-candidates.remove(i),i);
                    candidates.add(i, result.pop());
                }
            }
        }
    }
}

这个去重写的好别扭。。先把list里的元素都放到set里 list清空 再放回来。。
话说不知道有没有什么好方法啊。。。
list没有元素去重的方法吗。。是哪一个啊。。。>_<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值