当前位置:网站首页>LeetCode:39. Combined sum
LeetCode:39. Combined sum
2022-07-06 08:50:00 【Bertil】
To give you one No repeating elements Array of integers for candidates And a target integer target , find candidates You can make the sum of numbers as the target number target Of all Different combinations , And return... As a list . You can press In any order Return these combinations .
candidates Medium The same Numbers can Unlimited repeat selected . If the selected number of at least one number is different , Then the two combinations are different .
For a given input , Guarantee and for target The number of different combinations of is less than 150 individual .
Example 1:
Input :candidates = [2,3,6,7], target = 7
Output :[[2,2,3],[7]]
explain :
2 and 3 Can form a set of candidates ,2 + 2 + 3 = 7 . Be careful 2 You can use it multiple times .
7 Is also a candidate , 7 = 7 .
Only these two combinations .
Example 2:
Input : candidates = [2,3,5], target = 8
Output : [[2,2,2,2],[2,3,3],[3,5]]
Example 3:
Input : candidates = [2], target = 1
Output : []
Tips :
- 1 <= candidates.length <= 30
- 1 <= candidates[i] <= 200
- candidate Every element in is Different from each other
- 1 <= target <= 500
Their thinking
1. Using backtracking ( namely DFS Prune with constraints ): First, ascending the array , Then define recursive functions , But pay attention to : Numbers can be reused , So every recursion , The starting point of incoming is i
2. Use two conditions to prune , Recursive exit :
- Meet the conditions , Copy one into the result array
- The number of remaining targets is less than the current element , The latter elements will only be larger , Abandon this round directly
Backtracking and DFS, The main difference is , The backtracking method does not retain the complete tree structure in the solution process , While depth first search records the complete search tree
Code
/** * @param {number[]} candidates * @param {number} target * @return {number[][]} */
var combinationSum = function(candidates, target) {
const res = [];
// Ascending sort
candidates.sort((a, b) => a - b);
const search = (path, target, start) => {
if (target === 0) {
// Meet the conditions , Copy one into the result array
res.push([...path]);
return;
}
// Note that the starting point is start
for (let i = start; i < candidates.length; i++) {
// The number of remaining targets is less than the current element , The latter elements will only be larger , Abandon this round directly
if (target < candidates[i]) return;
path.push(candidates[i]);
// Numbers can be repeated , So the introduction i
search(path, target - candidates[i], i);
path.pop();
}
};
search([], target, 0);
return res;
};
边栏推荐
- LeetCode:剑指 Offer 03. 数组中重复的数字
- 电脑F1-F12用途
- LeetCode:剑指 Offer 42. 连续子数组的最大和
- Unsupported operation exception
- Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
- Promise 在uniapp的简单使用
- Revit secondary development Hof method calls transaction
- Detailed explanation of dynamic planning
- MySQL uninstallation and installation methods
- LeetCode:41. 缺失的第一个正数
猜你喜欢
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
企微服务商平台收费接口对接教程
可变长参数
JVM quick start
View computer devices in LAN
UML图记忆技巧
Esp8266-rtos IOT development
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Compétences en mémoire des graphiques UML
Roguelike game into crack the hardest hit areas, how to break the bureau?
随机推荐
Variable length parameter
Compétences en mémoire des graphiques UML
LeetCode:498. 对角线遍历
Detailed explanation of dynamic planning
[NVIDIA development board] FAQ (updated from time to time)
Roguelike game into crack the hardest hit areas, how to break the bureau?
Crash problem of Chrome browser
Indentation of tabs and spaces when writing programs for sublime text
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
The harm of game unpacking and the importance of resource encryption
What is CSRF (Cross Site Request Forgery)?
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
Tcp/ip protocol
企微服务商平台收费接口对接教程
[MySQL] limit implements paging
Navicat Premium 创建MySql 创建存储过程
查看局域网中电脑设备
@JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)