当前位置:网站首页>LeetCode:39. 组合总和
LeetCode:39. 组合总和
2022-07-06 08:44:00 【Bertil】
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
示例 1:
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []
提示:
- 1 <= candidates.length <= 30
- 1 <= candidates[i] <= 200
- candidate 中的每个元素都 互不相同
- 1 <= target <= 500
解题思路
1.使用回溯(即DFS用约束条件做剪枝):首先对数组进行升序,然后定义递归函数,但须注意:数字可以被重复使用,所以每次递归,传入的起点是i
2.利用两个条件做剪枝,即递归出口:
- 满足条件,拷贝一份放入结果数组
- 剩余的目标数小于当前元素,后面的元素只会更大,直接放弃此轮
回溯和DFS,其主要的区别是,回溯法在求解过程中不保留完整的树结构,而深度优先搜索则记下完整的搜索树
代码
/** * @param {number[]} candidates * @param {number} target * @return {number[][]} */
var combinationSum = function(candidates, target) {
const res = [];
// 升序排序
candidates.sort((a, b) => a - b);
const search = (path, target, start) => {
if (target === 0) {
// 满足条件,拷贝一份放入结果数组
res.push([...path]);
return;
}
// 注意起点为start
for (let i = start; i < candidates.length; i++) {
// 剩余的目标数小于当前元素,后面的元素只会更大,直接放弃此轮
if (target < candidates[i]) return;
path.push(candidates[i]);
// 数字可以重复,所以传入i
search(path, target - candidates[i], i);
path.pop();
}
};
search([], target, 0);
return res;
};
边栏推荐
- 如何有效地进行自动化测试?
- 个人电脑好用必备软件(使用过)
- torch建立的网络模型使用torchviz显示
- 深度剖析C语言数据在内存中的存储
- Trying to use is on a network resource that is unavailable
- 超高效!Swagger-Yapi的秘密
- Charging interface docking tutorial of enterprise and micro service provider platform
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- JVM performance tuning and practical basic theory - Part 1
- Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
猜你喜欢

Variable length parameter

【ROS】usb_cam相机标定

个人电脑好用必备软件(使用过)
![[MySQL] log](/img/e9/1617122888c096cf6aba2bdb88f0ef.png)
[MySQL] log

Excellent software testers have these abilities

角色动画(Character Animation)的现状与趋势

目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台

Sublime text using ctrl+b to run another program without closing other runs

Mobile phones and computers on the same LAN access each other, IIS settings
![[brush questions] top101 must be brushed in the interview of niuke.com](/img/55/5ca957e65d48e19dbac8043e89e7d9.png)
[brush questions] top101 must be brushed in the interview of niuke.com
随机推荐
hutool优雅解析URL链接并获取参数
Revit 二次开发 HOF 方式调用transaction
Is it safe to open an account in Zheshang futures?
Trying to use is on a network resource that is unavailable
Navicat Premium 创建MySql 创建存储过程
企微服务商平台收费接口对接教程
FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
Excellent software testers have these abilities
To effectively improve the quality of software products, find a third-party software evaluation organization
How to effectively conduct automated testing?
超高效!Swagger-Yapi的秘密
Process of obtaining the electronic version of academic qualifications of xuexin.com
LeetCode:673. 最长递增子序列的个数
Report on Market Research and investment prospects of China's silver powder industry (2022 Edition)
被破解毁掉的国产游戏之光
On the inverse order problem of 01 knapsack problem in one-dimensional state
Purpose of computer F1-F12
Light of domestic games destroyed by cracking
【Nvidia开发板】常见问题集 (不定时更新)