给定候选号码数组 (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

最新文章

  1. Jquery元素选取、常用方法
  2. universal image loader自己使用的一些感受
  3. 《C和指针(Pointer on c)》 学习笔记(转自:http://dsqiu.iteye.com/blog/1687944)
  4. HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效。
  5. Docker入门命令
  6. [Everyday Mathematics]20150221
  7. 【转】Bluetooth数据包捕获
  8. CoreCLR源码探索(二) new是什么
  9. java_反射_及其简单应用(2016-11-16)
  10. BDD框架之lettuce---python3.+安装报错
  11. 织梦安装过后出现&quot;...www/include/templets/default/index.htm Not Found!&quot;
  12. oc随笔五:NSArray
  13. 页面实现多个定时器(计时器)时选用NSTimer还是GCD?(干货不湿)
  14. Android PopupWindows
  15. [C]排序并插入
  16. 前端tab切换 和 validatejs表单验证插件
  17. tensorflow随机张量创建
  18. Netty Associated -- Channel
  19. swift设计模式学习 - 策略模式
  20. 从PSD到HTML,网页的实现

热门文章

  1. HttpClient_02_实现https协议
  2. codeforces 659F F. Polycarp and Hay(并查集+bfs)
  3. 【Shell】基础正则表示法及grep用法
  4. H5内容安全尺寸
  5. HDU4080Stammering Aliens(后缀数组+二分)
  6. BZOJ_4987_Tree_树形DP
  7. bzoj 3680(洛谷1337) 吊打XXX——模拟退火
  8. 洛谷P3372线段树模板1——线段树
  9. VijosP1595:学校网络(有向图变强连通图)
  10. orcale用户名的创建及权限设置