Java [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.
- 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]
解题思路:
跟前一个类似,不过每次选好当前元素后,就直接选下一个位置的元素了。并且在每次选元素之前,保证这次的元素与上次的元素不重合。
代码如下:
- public class Solution {
- public List<List<Integer>> combinationSum2(int[] candidates,
- int target) {
- Arrays.sort(candidates);
- List<List<Integer>> result = new ArrayList<List<Integer>>();
- getResult(result, new ArrayList<Integer>(), candidates, target, 0);
- return result;
- }
- public void getResult(List<List<Integer>> result,
- List<Integer> current, int[] candiates, int target, int start) {
- if (target > 0) {
- for (int i = start; i < candiates.length && target >= candiates[i]; i++) {
- if (i > start && candiates[i] == candiates[i - 1])
- continue;
- current.add(candiates[i]);
- getResult(result, current, candiates, target - candiates[i],
- i + 1);
- current.remove(current.size() - 1);
- }
- } else if (target == 0) {
- result.add(new ArrayList<Integer>(current));
- }
- }
- }
Java [Leetcode 40]Combination Sum II的更多相关文章
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- leetcode 40 Combination Sum II --- java
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 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(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
- [LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)
https://leetcode.wang/leetCode-40-Combination-Sum-II.html 描述 Given a collection of candidate numbers ...
随机推荐
- PAT Ranking (排名)
PAT Ranking (排名) Programming Ability Test (PAT) is organized by the College of Computer Science and ...
- php源代码安装常见错误与解决办法分享
错误:configure: error: libevent >= 1.4.11 could not be found 解决:yum -y install libevent libevent-de ...
- jsf2入门视频 教程
jsf2.0 入门视频 教程 需要的看下.初次录视频.还有很多需要完善. JSF交流QQ群84376982 JSF入门视频下载地址 http://pan.baidu.com/s/1jG3y4T4 ...
- 获取不变的UDID-b
iOS唯一标识的历史历程 iOS 6.0 在iOS6.0以前,是使用uniqueIdentifier来获取手机的唯一标识,后来苹果感觉这样会泄露用户隐藏,就封掉了这个方法: iOS 6.0系统新增了两 ...
- xcode 6.3 打包crash问题--参考
xcode升级6.3之后,有些项目会出现打包crash的问题,只要选择偏好设置,把source control全部禁用掉就可以了.
- appStore上传苹果应用程序软件发布流程(之前都是同事发,复制一份备用)
首先确定帐号是否能发布, https://developer.apple.com/account,如果你打开Provisioning Portal,然后点击DisTribution看到的是下图中那样, ...
- linux 下安装JDK1.7
安装JDK1.7 1. 打开网址http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u5-downloads-1591156.ht ...
- HIBERNATE一对一双向外键联合主键关联
HIBERNATE一对一双向外键联合主键关联: 一. 创建主键类:这个主键必须实现serializedable接口和重写其中的hashCode方法和equals方法:为主键类添加一个叫做@Embedd ...
- 互斥锁Mutex与信号量Semaphore的区别
转自互斥锁Mutex与信号量Semaphore的区别 多线程编程中,常常会遇到这两个概念:Mutex和Semaphore,两者之间区别如下: 有人做过如下类比: Mutex是一把钥匙,一个人拿了就可进 ...
- SqlServer日志
Sqlserver 2005日志查看.恢复工具 log exploer4.1 SqlServer2008 日志查看 系统函数 select * from fn_dblog(null,null) 参考 ...