-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path39.组合总和.js
39 lines (36 loc) · 994 Bytes
/
39.组合总和.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* @lc app=leetcode.cn id=39 lang=javascript
*
* [39] 组合总和
*
* 1. 从小到大排序, 记录下标, 遍历时下标不变或加一, 避免重复结果
* 2. 使用临时数字会比不断的创建新数组要更快, 且节约内存
*/
// @lc code=start
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum = function(candidates, target, start) {
candidates.sort((a, b) => a - b);
const result = [];
const path = [];
function dfs(target, start) {
if (target === 0) {
result.push([...path]);
} else if (target > 0) {
for (let i = start; i < candidates.length; i++) {
if (target < candidates[i]) {
break;
}
path.push(candidates[i]);
dfs(target - candidates[i], i);
path.pop();
}
}
}
dfs(target, 0);
return result;
};
// @lc code=end