leetcode 40 Combination Sum II --- java
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]
]
这道题就是39题的变化版本,这里每一个数字只许出现一次,并且最后的结果不允许重复,第一次只是将上一题的代码修改了边界条件,但结果不是很理想。
public class combinationSum2 {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
getResult(candidates,target,0,result,new ArrayList<Integer>());
return result;
}
public void getResult( int[] candidates, int target,int pos, List<List<Integer>> result,List<Integer> ans){
for( int i = pos;i <candidates.length; i++){
if( target == candidates[i]){
ans.add(candidates[i]);
result.add(new ArrayList<Integer>(ans));
ans.remove(ans.size()-1);
return;
}
else if(target > candidates[i]){
ans.add(candidates[i]);
getResult(candidates,target-candidates[i],i+1,result,ans);
ans.remove(ans.size()-1);
}else
return ;
}
}
/*
* 1.这道题和conbinationSum很相似,只不过不能使用重复的数字。
* 2.35+9
*/
}
之后发现,如果在getResult中,用数组代替List<Integer>那么会快很多,修改之后,做到了最快。
public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
getResult(candidates,target,0,result,new int[candidates.length],0);
return result;
}
public void getResult( int[] candidates, int target,int pos, List<List<Integer>> result,int[] ans,int num){
for( int i = pos;i <candidates.length; i++){
if( target == candidates[i]){
List<Integer> aa = new ArrayList<Integer>();
for( int ii =0; ii<num; ii++)
aa.add(ans[ii]);
aa.add(candidates[i]);
result.add(aa);
return;
}
else if(target > candidates[i]){
ans[num] = candidates[i];
getResult(candidates,target-candidates[i],i+1,result,ans,num+1);
while( i+1< candidates.length && candidates[i] == candidates[i+1])
i++;
}else
return ;
}
}
}
leetcode 40 Combination Sum II --- java的更多相关文章
- [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 ...
- 40. Combination Sum II (JAVA)
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- Java [Leetcode 40]Combination Sum II
题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinati ...
- 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也是正数. ...
随机推荐
- Hello Hibernate
Hibernate 一个框架; 一个 Java 领域的持久化框架; 一个 ORM 框架 ORM(Object/Relation Mapping): 对象/关系映射 –ORM的思想:将关系数据库中表中的 ...
- The Coco-Cola Store C(Contest #3 )
Once upon a time, there is a special coco-cola store. If you return three empty bottles to the shop, ...
- .net 小问题集结
1 .net中新建的项目,调试时,提示"由于未在web.config文件中启用调试,因此无法在调试模式下运行该页.您希望做什么?" 解决办法: 在web.config文件中,将&l ...
- == 和 isEqualToString的区别之备忘
== 比较的是指针 isEqualToString 比较的是指针指向的内容 比如: NSString * strA = @"abc"; NSString * strB = @&qu ...
- AJAX笔记
浏览器脚本——AJAX AJAX = 异步的 JavaScript 和 XML(Asynchronous JavaScript and XML). 是一种新的技术,它可以创建更好.更快且交互性更强的 ...
- stm32 dac库函数解读
1.简述: 12位数字输入,电压输出,DAC可以配置为8位或12位模式.有2个输出通道.在双DAC模式下,两个通道可以独立地工作. 特殊功能: 噪声波形生成,三角波形生成,外部触发转换,双DAC同时或 ...
- (转)Ratchet教程:meta与link标签
原文:http://www.w3cplus.com/mobile/meta-and-link-tags-for-ratchet.html Ratchet教程:meta与link标签 ...
- (转) mysql的连接,创建账号,修改密码
原文:http://blog.chinaunix.net/uid-20749043-id-1878306.html mysql的连接,创建账号,修改密码 2008-10-13 15:31:29 分类 ...
- Get Many Persimmon Trees_枚举&&二维树状数组
Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aiz ...
- JS手机定位地理位置
/** * 以下为html5代码,获取地理位置 */ /** * 设置地址 */ function setAddress(json) { var position = document.getElem ...