Skip to content

Commit 621ad65

Browse files
committed
update: 40
1 parent 15a2ad1 commit 621ad65

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
3131
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/) | [JavaScript](./src/search-for-a-range/res.js)|Medium|
3232
|35|[Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [JavaScript](./src/search-insert-position/res.js)|Easy|
3333
|39|[Combination Sum](https://leetcode.com/problems/combination-sum/) | [JavaScript](./src/combination-sum/res.js)|Medium|
34+
|40|[Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [JavaScript](./src/combination-sum-ii/res.js)|Medium|
3435
|41|[First Missing Positive](https://leetcode.com/problems/first-missing-positive/description/) | [JavaScript](./src/first-missing-positive/res.js)|Hard|
3536
|42|[ Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/description/) | [JavaScript](./src/trapping-rain-water/res.js)|Hard|
3637
|43|[Multiply Strings](https://leetcode.com/problems/multiply-strings/) | [JavaScript](./src/multiply-strings/res.js)|Medium|

src/combination-sum-ii/res.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[]} candidates
3+
* @param {number} target
4+
* @return {number[][]}
5+
*/
6+
var combinationSum2 = function(candidates, target) {
7+
candidates.sort((a, b) => b-a);
8+
const res = [];
9+
10+
const calCombs = (candis, resArr, targ) => {
11+
if (!candis.length) return ;
12+
const ele = candis[0];
13+
14+
if (targ === ele) {
15+
res.push([...resArr, ele]);
16+
} else if (targ - ele > 0) {
17+
calCombs(candis.slice(1), [...resArr, ele], targ-ele);
18+
}
19+
20+
calCombs(candis.slice(1), [...resArr], targ);
21+
}
22+
23+
calCombs(candidates, [], target);
24+
25+
// console.log(res);
26+
return res;
27+
};

src/combination-sum/res.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,15 @@ var combinationSum = function(candidates, target) {
1414

1515
if (targ === ele) {
1616
res.push([...resArr, ele]);
17-
} else {
18-
calCombs(candis.slice(1), [...resArr], targ);
17+
} else if (targ - ele > 0) {
18+
calCombs(candis.slice(), [...resArr, ele], targ-ele);
1919
}
2020

21-
if (targ - ele > 0) {
22-
calCombs(candis, [...resArr, ele], targ-ele);
23-
}
21+
calCombs(candis.slice(1), [...resArr], targ);
2422
}
2523

2624
calCombs(candidates, [], target);
2725

28-
console.log(res);
26+
// console.log(res);
2927
return res;
3028
};
31-
32-
combinationSum([2,3,6,7],
33-
7);

0 commit comments

Comments
 (0)