Skip to content

Commit

Permalink
添加39. 组合总和JavaScript版本
Browse files Browse the repository at this point in the history
  • Loading branch information
flames519 committed Jun 5, 2021
1 parent 7cbf0b1 commit d6955eb
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions problems/0039.组合总和.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,30 +286,30 @@ class Solution:
```
Go:

JavaScript
JavaScript:

```js
var strStr = function (haystack, needle) {
if (needle === '') {
return 0;
}

let hayslen = haystack.length;
let needlen = needle.length;

if (haystack === '' || hayslen < needlen) {
return -1;
}

for (let i = 0; i <= hayslen - needlen; i++) {
if (haystack[i] === needle[0]) {
if (haystack.substr(i, needlen) === needle) {
return i;
}
}
if (i === hayslen - needlen) {
return -1;
var combinationSum = function(candidates, target) {
const res = [], path = [];
candidates.sort(); // 排序
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;
path.push(n);
sum += n;
backtracking(i, sum);
path.pop();
sum -= n;
}
}
}
};
```

Expand Down

0 comments on commit d6955eb

Please sign in to comment.