Middle-题目79:40. Combination Sum II

原创 2016年05月31日 16:58:45

题目原文:
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]
题目大意:
Middle-题目54的基础上,现在允许重复数字出现,但每个重复数字只能用一次,还是求所有的排列情况。
题目分析:
Middle-题目54的基础上修改,还是先排序,相同的元素分别统计,然后去重的思路与Middle-题目76相同。
源码:(language:java)

public class Solution {
    public  List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        backtrack(list, new ArrayList<Integer>(), candidates, target, 0);
        return list;
    }
    private  void backtrack(List<List<Integer>> list, List<Integer> sublist, int[] candidates, int target, int startPos) {
        if(target == 0) {
            list.add(new ArrayList<Integer>(sublist));
            return;
        }
        else {
            for(int i = startPos; i < candidates.length; i++) {
                int temp = candidates[i];
                if(temp <= target) {
                    int iCount = 1;
                    while(i+1!=candidates.length&&candidates[i]==candidates[i+1]) {
                        iCount++;
                        i++;
                    }
                    for(int j = 1;j<=iCount;j++) {
                        for(int k = 0;k<j;k++)
                            sublist.add(temp);
                        backtrack(list,sublist,candidates,target-temp*j,i+1);
                        for(int k = 0;k<j;k++)
                            sublist.remove(sublist.size()-1);
                    }
                }
            }
        }
    }
}

成绩:
9ms,beats 62.50%,众数5ms,18.18%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

DAY19:leetcode #40 Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in...

[leetcode] 40. Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in...

leetcode-java-40. Combination Sum II

/* Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w...

40-m-Combination Sum II

求指定和的组合,但个个数只能用一次。求和一的变种,有了一的经验,二式可以沿用一式的backtracking格式,由于每个数只能用一次,那么控制参数i每次递增1就可以了。另一个要注意的是可能有重复项而重...

[LeetCode]problem 40. Combination Sum II

TAG递归回溯组合数和link方法与Combination Sum 相比, 每个数仅能被使用一次 候选集不再是set,而是collections,就是可以包含重复数字了 怎么办呢?针对第一个,非常简单...

40. Combination Sum II---找出和为target的子数组

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...

LeetCode --- 40. Combination Sum II

题目链接:Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find a...

【leetcode】40Combination Sum II(回溯方法)

题目大意: 给出一个候选数字集(其中可能有重复的数字),还有一个目标数字,数字均为正整数,从候选数字集中选取数字,候选集中每个元素最多选一次(若相同数字有多个则可多次选取),要求输出所有满足和为目标数...

40. Combination Sum II

深度优先搜索,回溯

(Java)LeetCode-40. Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)