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]

题目大意:跟上一题类似,但是有点区别就是候选集中的元素只能出现一次。

解题思路:每次从当前元素的下一个开始计算sum,并把candidate加入List,并且跳过重复元素。

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
if (candidates == null || candidates.length == 0) {
return res;
}
Deque<Integer> tmp = new ArrayDeque<>();
Arrays.sort(candidates);
helper(res, tmp, 0, target, candidates);
return res;
} private void helper(List<List<Integer>> res, Deque<Integer> tmp, int start, int target, int[] candidate) {
if (target == 0) {
res.add(new ArrayList<>(tmp));
// System.out.println(tmp);
return;
}
for (int i = start; i < candidate.length && target >= candidate[i]; i++) {
tmp.addLast(candidate[i]);
helper(res, tmp, i + 1, target - candidate[i], candidate);
tmp.removeLast();
while (i < candidate.length - 1 && candidate[i + 1] == candidate[i]) {
i++;
}
}
}

Combination Sum II —— LeetCode的更多相关文章

  1. Combination Sum II leetcode java

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  2. Combination Sum II [LeetCode]

    Problem description: http://oj.leetcode.com/problems/combination-sum-ii/ Basic idea: use recursive a ...

  3. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

  4. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  5. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  6. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  7. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  8. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  9. 【LeetCode】40. Combination Sum II (2 solutions)

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

随机推荐

  1. json 序列化的两种方式

    JavaScriptSerializer Serializer = new JavaScriptSerializer(); ResultData<EUserData> resultMode ...

  2. PetaPoco 存储过程

    1 执行不带参数的存储过程 public List<dynamic> ceshiProc() { string sql = @"EXEC [dbo].[p_ceshi1]&quo ...

  3. Activity.startManagingCursor方法

    http://blog.sina.com.cn/s/blog_6f14deb60100wd2n.html 总结一下Activity.startManagingCursor方法: 转 我们将获得的Cur ...

  4. 表单提交对chrome记住密码的影响

    在处理注册.登录等含有用户名,密码的元素的表单时,chrome会主动的提示记住密码,然而这个功能在用户名的选择上真是耐人寻味,它总是寻找离password input控件最近的那一个文本框的内容,作为 ...

  5. 怎样在官网上下载xcode7.2

    其实我觉得还是有必要就这个写一篇论文的  以证明自己真的是个菜鸟 首先进入苹果开发者官网 https://developer.apple.com/ 选择 resource 然后 点击加号  然后下载就 ...

  6. 搞一个app需要多久?

    //转载文章,看后有感 我有些尴尬地拿着水杯,正对面坐着来访的王总,他是在别处打拼的人,这几年据说收获颇丰,见移动互联网如火如荼,自然也想着要进来干一场,尽管王总从事的行当也算跟IT沾边,但毕竟太长时 ...

  7. php输出echo、print、print_r、printf、sprintf、var_dump的区别比较

    本篇文章是对php输出echo.print.print_r.printf.sprintf.var_dump的区别进行了详细的分析介绍,需要的朋友参考下     用.net开发已经5年了,最近突然想接触 ...

  8. 258. Add Digits(C++)

    258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has ...

  9. 头一回发博客,来分享个有关C++类型萃取的编写技巧

    废话不多说,上来贴代码最实在,哈哈! 以下代码量有点多,不过这都是在下一手一手敲出来的,小巧好用,把以下代码复制出来,放到相应的hpp文件即可,VS,GCC下均能编译通过 #include<io ...

  10. CentOS7开机启动管理systemd简介及使用

    systemd提供更优秀的框架以表示系统服务间的依赖关系实现系统初始化时服务的并行启动,同时达到降低Shell的系统开销的效果systemd的目标是:尽可能启动更少进程:尽可能将更多进程并行启动.sy ...