LeetCode40.组合总和|| JavaScript
2024-08-26 03:04:57
给定一个数组 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()
}
}
}
};
最新文章
- android 中layer-list的用法
- 【leetcode】Search in Rotated Sorted Array II
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
- A20的板子笔记
- SWIFT UITableView的基本用法
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) D. Sea Battle 模拟
- 《C++Primer》复习——with C++11 [2]
- 统计 p-value 含义
- iOS消息推送机制的实现
- mysql 数据sqoop到hive 步骤
- c++之 scanf 接收用户输入内容
- Mybatis篇总结
- SSD(single shot multibox detector)
- Java 实现追加excle文件内容
- Android抓包方法 之Fiddler代理
- HTTP协议学习【转】
- HDU 2176 基础NIM 输出方案
- java多线程技术之条件变量
- js继承摘要
- Custom Ribbon in SharePoint 2010 &; which not wrok when migrate from 2010 to 2013
热门文章
- [SD2015]序列统计——solution
- css 关闭按钮实现
- JS计算距当前时间的时间差
- 为订阅内虚拟机批量安装并配置 Microsoft Anti-Malware 扩展
- 打通版微社区(3):在Web服务器上部署memcache For DZ3.2
- [mutt] Configure mutt to receive email via IMAP and send via SMTP
- Win10系统创建关机快捷方式和快捷键的方法,实现一键关机
- Java实例---简单的超市管理系统
- MVC 入门-MvcMovie
- 关于使用 CALayer 中 mask 的一些技巧