040 Combination Sum II 组合总和 II
2024-09-03 12:51:12
给定候选号码数组 (C) 和目标总和数 (T),找出 C 中候选号码总和为 T 的所有唯一组合。
C 中的每个数字只能在组合中使用一次。
注意:
所有数字(包括目标)都是正整数。
解决方案集不能包含重复的组合。
例如,给定候选集合 [10, 1, 2, 7, 6, 1, 5] 和目标总和数 8,
可行的集合是:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
详见:https://leetcode.com/problems/combination-sum-ii/description/
Java实现:
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
List<Integer> out=new ArrayList<Integer>();
Arrays.sort(candidates);
helper(candidates,target,0,out,res);
return res;
}
private void helper(int[] candidates, int target,int start,List<Integer> out,List<List<Integer>> res){
if(target<0){
return;
}
if(target==0){
res.add(new ArrayList<Integer>(out));
}else{
for(int i=start;i<candidates.length;++i){
if(i>start&&candidates[i]==candidates[i-1]){
continue;
}
out.add(candidates[i]);
helper(candidates,target-candidates[i],i+1,out,res);
out.remove(out.size()-1);
}
}
}
}
参考:http://www.cnblogs.com/grandyang/p/4419386.html
最新文章
- Jquery元素选取、常用方法
- universal image loader自己使用的一些感受
- 《C和指针(Pointer on c)》 学习笔记(转自:http://dsqiu.iteye.com/blog/1687944)
- HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效。
- Docker入门命令
- [Everyday Mathematics]20150221
- 【转】Bluetooth数据包捕获
- CoreCLR源码探索(二) new是什么
- java_反射_及其简单应用(2016-11-16)
- BDD框架之lettuce---python3.+安装报错
- 织梦安装过后出现";...www/include/templets/default/index.htm Not Found!";
- oc随笔五:NSArray
- 页面实现多个定时器(计时器)时选用NSTimer还是GCD?(干货不湿)
- Android PopupWindows
- [C]排序并插入
- 前端tab切换 和 validatejs表单验证插件
- tensorflow随机张量创建
- Netty Associated -- Channel
- swift设计模式学习 - 策略模式
- 从PSD到HTML,网页的实现
热门文章
- HttpClient_02_实现https协议
- codeforces 659F F. Polycarp and Hay(并查集+bfs)
- 【Shell】基础正则表示法及grep用法
- H5内容安全尺寸
- HDU4080Stammering Aliens(后缀数组+二分)
- BZOJ_4987_Tree_树形DP
- bzoj 3680(洛谷1337) 吊打XXX——模拟退火
- 洛谷P3372线段树模板1——线段树
- VijosP1595:学校网络(有向图变强连通图)
- orcale用户名的创建及权限设置