题目描述:

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]

解题思路:

跟前一个类似,不过每次选好当前元素后,就直接选下一个位置的元素了。并且在每次选元素之前,保证这次的元素与上次的元素不重合。

代码如下:

  1. public class Solution {
  2. public List<List<Integer>> combinationSum2(int[] candidates,
  3. int target) {
  4. Arrays.sort(candidates);
  5. List<List<Integer>> result = new ArrayList<List<Integer>>();
  6. getResult(result, new ArrayList<Integer>(), candidates, target, 0);
  7. return result;
  8. }
  9.  
  10. public void getResult(List<List<Integer>> result,
  11. List<Integer> current, int[] candiates, int target, int start) {
  12. if (target > 0) {
  13. for (int i = start; i < candiates.length && target >= candiates[i]; i++) {
  14. if (i > start && candiates[i] == candiates[i - 1])
  15. continue;
  16. current.add(candiates[i]);
  17. getResult(result, current, candiates, target - candiates[i],
  18. i + 1);
  19. current.remove(current.size() - 1);
  20. }
  21. } else if (target == 0) {
  22. result.add(new ArrayList<Integer>(current));
  23. }
  24. }
  25. }

  

Java [Leetcode 40]Combination Sum II的更多相关文章

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

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

  2. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  3. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  4. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. leetcode 40 Combination Sum II --- java

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

  6. LeetCode 40. Combination Sum II (组合的和之二)

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

  7. Leetcode 40. Combination Sum II

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

  8. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

    题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数. ...

  9. [LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)

    https://leetcode.wang/leetCode-40-Combination-Sum-II.html 描述 Given a collection of candidate numbers ...

随机推荐

  1. PAT Ranking (排名)

    PAT Ranking (排名) Programming Ability Test (PAT) is organized by the College of Computer Science and ...

  2. php源代码安装常见错误与解决办法分享

    错误:configure: error: libevent >= 1.4.11 could not be found 解决:yum -y install libevent libevent-de ...

  3. jsf2入门视频 教程

    jsf2.0 入门视频 教程   需要的看下.初次录视频.还有很多需要完善. JSF交流QQ群84376982 JSF入门视频下载地址  http://pan.baidu.com/s/1jG3y4T4 ...

  4. 获取不变的UDID-b

    iOS唯一标识的历史历程 iOS 6.0 在iOS6.0以前,是使用uniqueIdentifier来获取手机的唯一标识,后来苹果感觉这样会泄露用户隐藏,就封掉了这个方法: iOS 6.0系统新增了两 ...

  5. xcode 6.3 打包crash问题--参考

    xcode升级6.3之后,有些项目会出现打包crash的问题,只要选择偏好设置,把source control全部禁用掉就可以了.

  6. appStore上传苹果应用程序软件发布流程(之前都是同事发,复制一份备用)

    首先确定帐号是否能发布, https://developer.apple.com/account,如果你打开Provisioning Portal,然后点击DisTribution看到的是下图中那样, ...

  7. linux 下安装JDK1.7

    安装JDK1.7 1. 打开网址http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u5-downloads-1591156.ht ...

  8. HIBERNATE一对一双向外键联合主键关联

    HIBERNATE一对一双向外键联合主键关联: 一. 创建主键类:这个主键必须实现serializedable接口和重写其中的hashCode方法和equals方法:为主键类添加一个叫做@Embedd ...

  9. 互斥锁Mutex与信号量Semaphore的区别

    转自互斥锁Mutex与信号量Semaphore的区别 多线程编程中,常常会遇到这两个概念:Mutex和Semaphore,两者之间区别如下: 有人做过如下类比: Mutex是一把钥匙,一个人拿了就可进 ...

  10. SqlServer日志

    Sqlserver 2005日志查看.恢复工具 log exploer4.1 SqlServer2008 日志查看  系统函数 select * from fn_dblog(null,null) 参考 ...