mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
添加39. 组合总和JavaScript版本
This commit is contained in:
@ -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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (i === hayslen - needlen) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user