mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加40.组合总和IIJavaScript版本
This commit is contained in:
@ -293,7 +293,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
Python:
|
Python:
|
||||||
```python3
|
```py
|
||||||
class Solution:
|
class Solution:
|
||||||
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
|
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
|
||||||
res = []
|
res = []
|
||||||
@ -314,7 +314,39 @@ class Solution:
|
|||||||
```
|
```
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
javaScript:
|
||||||
|
|
||||||
|
```js
|
||||||
|
/**
|
||||||
|
* @param {number[]} candidates
|
||||||
|
* @param {number} target
|
||||||
|
* @return {number[][]}
|
||||||
|
*/
|
||||||
|
var combinationSum2 = function(candidates, target) {
|
||||||
|
const res = []; path = [], len = candidates.length;
|
||||||
|
candidates.sort();
|
||||||
|
backtracking(0, 0);
|
||||||
|
return res;
|
||||||
|
function backtracking(sum, i) {
|
||||||
|
if (sum > target) return;
|
||||||
|
if (sum === target) {
|
||||||
|
res.push(Array.from(path));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let f = -1;
|
||||||
|
for(let j = i; j < len; j++) {
|
||||||
|
const n = candidates[j];
|
||||||
|
if(n > target - sum || n === f) continue;
|
||||||
|
path.push(n);
|
||||||
|
sum += n;
|
||||||
|
f = n;
|
||||||
|
backtracking(sum, j + 1);
|
||||||
|
path.pop();
|
||||||
|
sum -= n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user