给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用一次。

说明:

  • 所有数字(包括目标数)都是正整数。
  • 解集不能包含重复的组合。

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

示例 2:

输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
  [1,2,2],
  [5]
]
【2,2,3,6,7】 target=8 按上一题的代码,会出现 [[2,6][2,6]]故当候选集中相邻两个位置的值相等时,跳过。
 class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList();
if(candidates.length == 0 || candidates == null)return res;
Arrays.sort(candidates);
helper(res,new ArrayList(),candidates,target,0);
return res;
}
public void helper(List<List<Integer>> res,List<Integer> list,int[] candidates,int target,int start){
if(target < 0)return;
if(target == 0){
res.add(new ArrayList(list));
return;
}
for(int i = start;i < candidates.length;i++){
if(i != start && candidates[i] == candidates[i - 1]) continue;
list.add(candidates[i]);
helper(res,list,candidates,target - candidates[i],i+1);
list.remove(list.size() - 1);
}
}
}

2019-05-01 20:32:34

最新文章

  1. JVM调优总结
  2. Repeater 控件
  3. ubuntu同时安装qt4.8和qt5.7
  4. windows环境同时连多个openvpn配置
  5. [原]1856-More is better-基础并查集
  6. 算法:最大子数组own
  7. sublime package
  8. COM 参数有in, out ,retval
  9. asp.net MVC日志插件Log4Net学习笔记一:保存日志到本地
  10. HDU-4665 Unshuffle 搜索 | 2-SAT
  11. Spark常用函数讲解之键值RDD转换
  12. Location-aware Associated Data Placement for Geo-distributed Data-intensive Applications--INFOCOM 2015
  13. tomcat配置https协议
  14. java的socket通信
  15. JavaScript中对象数组 根据某个属性值 然后push到新的数组
  16. MFC中打开选择文件夹对话框,并将选中的文件夹地址显示在编辑框中
  17. 仿微信的IM聊天时间显示格式(含iOS/Android/Web实现)[图文+源码]
  18. vue中data中引用本地图片报错404
  19. 在webpack3里使用uglifyjs
  20. 《HTTP权威指南》读书笔记(一)

热门文章

  1. Basic Model Theory of XPath on Data Trees
  2. 服务器上安装搭建node环境
  3. vue-element-template模板项目使用记录(持续更新)
  4. Delphi XE2 之 FireMonkey 入门(5) - TAlphaColor
  5. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第3节 Random类_10-练习一_生成1-n之间
  6. 《计算机程式设计》Week5 课堂笔记
  7. Temporal-Difference Control: SARSA and Q-Learning
  8. python每日一练:0004题
  9. Proteus报错处理经验:power rails ‘GND’ and &#39;VCC/VDD&#39; are interconnected in net VCC
  10. mysql续集(查询部分)