diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index fa135d8d..aefee698 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -137,7 +137,7 @@ for (int i = 0; i < letters.size(); i++) { 关键地方都讲完了,按照[关于回溯算法,你该了解这些!](https://mp.weixin.qq.com/s/gjSgJbNbd1eAA5WkA-HeWw)中的回溯法模板,不难写出如下C++代码: -``` +```c++ // 版本一 class Solution { private: @@ -183,7 +183,7 @@ public: 一些写法,是把回溯的过程放在递归函数里了,例如如下代码,我可以写成这样:(注意注释中不一样的地方) -``` +```c++ // 版本二 class Solution { private: @@ -319,7 +319,7 @@ class Solution: python3: -```python3 +```py class Solution: def letterCombinations(self, digits: str) -> List[str]: self.s = "" @@ -342,6 +342,33 @@ class Solution: 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(); + } + + } +}; +``` diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md index e1023ec9..e3e4a117 100644 --- a/problems/0039.组合总和.md +++ b/problems/0039.组合总和.md @@ -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; - } - } }; ``` diff --git a/problems/0040.组合总和II.md b/problems/0040.组合总和II.md index 4d2996f5..a462b524 100644 --- a/problems/0040.组合总和II.md +++ b/problems/0040.组合总和II.md @@ -293,7 +293,7 @@ class Solution { } ``` Python: -```python3 +```py class Solution: def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: res = [] @@ -314,7 +314,39 @@ class Solution: ``` 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; + } + } +}; +``` ----------------------- diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index 31acf4a5..d3e82f09 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -147,7 +147,7 @@ public: Java: -``` +```java class Solution { List> result = new ArrayList<>(); LinkedList 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(); + } + } +}; +``` + diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 3c8e5d9d..a8b9a215 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -338,6 +338,35 @@ class Solution(object): 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) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index c722af37..9d23fd13 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -292,7 +292,7 @@ class Solution { ``` Python: -```python3 +```py class Solution: def partition(self, s: str) -> List[List[str]]: res = [] @@ -313,7 +313,38 @@ class Solution: 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(); + } + } +}; +``` ----------------------- diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index 0aef7aec..9f75b23d 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -180,7 +180,7 @@ if (sum > targetSum) { // 剪枝操作 最后C++代码如下: -``` +```c++ class Solution { private: vector> result; // 存放结果集 @@ -262,7 +262,7 @@ class Solution { ``` Python: -```python3 +```py class Solution: def combinationSum3(self, k: int, n: int) -> List[List[int]]: res = [] #存放结果集 @@ -284,6 +284,44 @@ class Solution: 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; + } + } +}; +```