给定一个数组 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]
] 答案参考:
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum2 = function(candidates, target) { var item=[],path=[];
candidates=candidates.sort(function(a,b){return a-b})
GG(candidates,target,target,item,path,0)
return item
function GG(candidates,target,remain,item,path,start){
if(remain<0)
return;
if(remain==0){
path=path.slice()
item.push(path);
}
else{
for(var i=start;i<candidates.length;i++){
if(i>start&&candidates[i]==candidates[i-1])
continue;
path.push(candidates[i])
GG(candidates,target,remain-candidates[i],item,path,i+1)
path.pop()
}
}
}
};

最新文章

  1. android 中layer-list的用法
  2. 【leetcode】Search in Rotated Sorted Array II
  3. LeetCode 3 Longest Substring Without Repeating Characters 解题报告
  4. A20的板子笔记
  5. SWIFT UITableView的基本用法
  6. Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) D. Sea Battle 模拟
  7. 《C++Primer》复习——with C++11 [2]
  8. 统计 p-value 含义
  9. iOS消息推送机制的实现
  10. mysql 数据sqoop到hive 步骤
  11. c++之 scanf 接收用户输入内容
  12. Mybatis篇总结
  13. SSD(single shot multibox detector)
  14. Java 实现追加excle文件内容
  15. Android抓包方法 之Fiddler代理
  16. HTTP协议学习【转】
  17. HDU 2176 基础NIM 输出方案
  18. java多线程技术之条件变量
  19. js继承摘要
  20. Custom Ribbon in SharePoint 2010 &amp; which not wrok when migrate from 2010 to 2013

热门文章

  1. [SD2015]序列统计——solution
  2. css 关闭按钮实现
  3. JS计算距当前时间的时间差
  4. 为订阅内虚拟机批量安装并配置 Microsoft Anti-Malware 扩展
  5. 打通版微社区(3):在Web服务器上部署memcache For DZ3.2
  6. [mutt] Configure mutt to receive email via IMAP and send via SMTP
  7. Win10系统创建关机快捷方式和快捷键的方法,实现一键关机
  8. Java实例---简单的超市管理系统
  9. MVC 入门-MvcMovie
  10. 关于使用 CALayer 中 mask 的一些技巧