Merge pull request #325 from flames519/master

回溯,组合分割 JavaScript版本
This commit is contained in:
Carl Sun
2021-06-05 21:11:51 +08:00
committed by GitHub
7 changed files with 209 additions and 30 deletions

View File

@ -137,7 +137,7 @@ for (int i = 0; i < letters.size(); i++) {
关键地方都讲完了,按照[关于回溯算法,你该了解这些!](https://mp.weixin.qq.com/s/gjSgJbNbd1eAA5WkA-HeWw)中的回溯法模板不难写出如下C++代码: 关键地方都讲完了,按照[关于回溯算法,你该了解这些!](https://mp.weixin.qq.com/s/gjSgJbNbd1eAA5WkA-HeWw)中的回溯法模板不难写出如下C++代码:
``` ```c++
// 版本一 // 版本一
class Solution { class Solution {
private: private:
@ -183,7 +183,7 @@ public:
一些写法,是把回溯的过程放在递归函数里了,例如如下代码,我可以写成这样:(注意注释中不一样的地方) 一些写法,是把回溯的过程放在递归函数里了,例如如下代码,我可以写成这样:(注意注释中不一样的地方)
``` ```c++
// 版本二 // 版本二
class Solution { class Solution {
private: private:
@ -319,7 +319,7 @@ class Solution:
python3 python3
```python3 ```py
class Solution: class Solution:
def letterCombinations(self, digits: str) -> List[str]: def letterCombinations(self, digits: str) -> List[str]:
self.s = "" self.s = ""
@ -342,6 +342,33 @@ class Solution:
Go Go
javaScript
```js
var letterCombinations = function(digits) {
const k = digits.length;
const map = ["","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"];
if(!k) return [];
if(k === 1) return map[digits].split("");
const res = [], path = [];
backtracking(digits, k, 0);
return res;
function backtracking(n, k, a) {
if(path.length === k) {
res.push(path.join(""));
return;
}
for(const v of map[n[a]]) {
path.push(v);
backtracking(n, k, a + 1);
path.pop();
}
}
};
```

View File

@ -286,30 +286,30 @@ class Solution:
``` ```
Go Go
JavaScript JavaScript
```js ```js
var strStr = function (haystack, needle) { var combinationSum = function(candidates, target) {
if (needle === '') { const res = [], path = [];
return 0; candidates.sort(); // 排序
} backtracking(0, 0);
return res;
let hayslen = haystack.length; function backtracking(j, sum) {
let needlen = needle.length; if (sum > target) return;
if (sum === target) {
if (haystack === '' || hayslen < needlen) { res.push(Array.from(path));
return -1; return;
} }
for(let i = j; i < candidates.length; i++ ) {
for (let i = 0; i <= hayslen - needlen; i++) { const n = candidates[i];
if (haystack[i] === needle[0]) { if(n > target - sum) continue;
if (haystack.substr(i, needlen) === needle) { path.push(n);
return i; sum += n;
} backtracking(i, sum);
path.pop();
sum -= n;
}
} }
if (i === hayslen - needlen) {
return -1;
}
}
}; };
``` ```

View File

@ -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;
}
}
};
```
----------------------- -----------------------

View File

@ -147,7 +147,7 @@ public:
Java Java
``` ```java
class Solution { class Solution {
List<List<Integer>> result = new ArrayList<>(); List<List<Integer>> result = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>(); LinkedList<Integer> path = new LinkedList<>();
@ -220,6 +220,28 @@ func backtrack(n,k,start int,track []int){
} }
``` ```
javaScript:
```js
var combine = function(n, k) {
const res = [], path = [];
backtracking(n, k, 1);
return res;
function backtracking (n, k, i){
const len = path.length;
if(len === k) {
res.push(Array.from(path));
return;
}
for(let a = i; a <= n + len - k + 1; a++) {
path.push(a);
backtracking(n, k, a + 1);
path.pop();
}
}
};
```

View File

@ -338,6 +338,35 @@ class Solution(object):
return ans``` return ans```
``` ```
JavaScript
```js
/**
* @param {string} s
* @return {string[]}
*/
var restoreIpAddresses = function(s) {
const res = [], path = [];
backtracking(0, 0)
return res;
function backtracking(i) {
const len = path.length;
if(len > 4) return;
if(len === 4 && i === s.length) {
res.push(path.join("."));
return;
}
for(let j = i; j < s.length; j++) {
const str = s.substr(i, j - i + 1);
if(str.length > 3 || +str > 255) break;
if(str.length > 1 && str[0] === "0") break;
path.push(str);
backtracking(j + 1);
path.pop()
}
}
};
```
----------------------- -----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

View File

@ -292,7 +292,7 @@ class Solution {
``` ```
Python Python
```python3 ```py
class Solution: class Solution:
def partition(self, s: str) -> List[List[str]]: def partition(self, s: str) -> List[List[str]]:
res = [] res = []
@ -313,7 +313,38 @@ class Solution:
Go Go
javaScript
```js
/**
* @param {string} s
* @return {string[][]}
*/
const isPalindrome = (s, l, r) => {
for (let i = l, j = r; i < j; i++, j--) {
if(s[i] !== s[j]) return false;
}
return true;
}
var partition = function(s) {
const res = [], path = [], len = s.length;
backtracking(0);
return res;
function backtracking(i) {
if(i >= len) {
res.push(Array.from(path));
return;
}
for(let j = i; j < len; j++) {
if(!isPalindrome(s, i, j)) continue;
path.push(s.substr(i, j - i + 1));
backtracking(j + 1);
path.pop();
}
}
};
```
----------------------- -----------------------

View File

@ -180,7 +180,7 @@ if (sum > targetSum) { // 剪枝操作
最后C++代码如下: 最后C++代码如下:
``` ```c++
class Solution { class Solution {
private: private:
vector<vector<int>> result; // 存放结果集 vector<vector<int>> result; // 存放结果集
@ -262,7 +262,7 @@ class Solution {
``` ```
Python Python
```python3 ```py
class Solution: class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]: def combinationSum3(self, k: int, n: int) -> List[List[int]]:
res = [] #存放结果集 res = [] #存放结果集
@ -284,6 +284,44 @@ class Solution:
Go Go
javaScript:
```js
// 等差数列
var maxV = k => k * (9 + 10 - k) / 2;
var minV = k => k * (1 + k) / 2;
var combinationSum3 = function(k, n) {
if (k > 9 || k < 1) return [];
// if (n > maxV(k) || n < minV(k)) return [];
// if (n === maxV(k)) return [Array.from({length: k}).map((v, i) => 9 - i)];
// if (n === minV(k)) return [Array.from({length: k}).map((v, i) => i + 1)];
const res = [], path = [];
backtracking(k, n, 1, 0);
return res;
function backtracking(k, n, i, sum){
const len = path.length;
if (len > k || sum > n) return;
if (maxV(k - len) < n - sum) return;
if (minV(k - len) > n - sum) return;
if(len === k && sum == n) {
res.push(Array.from(path));
return;
}
const min = Math.min(n - sum, 9 + len - k + 1);
for(let a = i; a <= min; a++) {
path.push(a);
sum += a;
backtracking(k, n, a + 1, sum);
path.pop();
sum -= a;
}
}
};
```