Skip to content

Commit

Permalink
优化JS版本代码 (0039.组合总和.md)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anmizi authored Apr 29, 2022
1 parent f409384 commit 31eb619
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions problems/0039.组合总和.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,18 +370,17 @@ func backtracking(startIndex,sum,target int,candidates,trcak []int,res *[][]int)
```js
var combinationSum = function(candidates, target) {
const res = [], path = [];
candidates.sort(); // 排序
candidates.sort((a,b)=>a-b); // 排序
backtracking(0, 0);
return res;
function backtracking(j, sum) {
if (sum > target) return;
if (sum === target) {
res.push(Array.from(path));
return;
}
for(let i = j; i < candidates.length; i++ ) {
const n = candidates[i];
if(n > target - sum) continue;
if(n > target - sum) break;
path.push(n);
sum += n;
backtracking(i, sum);
Expand Down

0 comments on commit 31eb619

Please sign in to comment.