提交 ac1aba7c 编写于 作者: L luzhipeng

feat: backtrack

上级 e0914431
<mxfile modified="2019-04-24T02:09:19.313Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" etag="RcNPuPlIJm0vKz2RJuEk" version="10.6.3"><diagram id="rd1BCuUelQabY7rJ9Wer" name="第 1 页">7ZxRk6I4EMc/jY9rQQIBHkcd7x5m665qHu7ephiISi0YD+Lp7Ke/oEGBzuy5iia6vIzShBj+v3TTaVIzwONs+1serhZfWUzTAbLi7QBPBgjZOPDFR2n52Ft8x9kb5nkSy0ZHw2vynUqjJa3rJKZFoyFnLOXJqmmM2HJJI96whXnONs1mM5Y2f3UVzikwvEZhCq1/JTFfyLtA3tH+O03mi+qXbRLsz2Rh1VjeSbEIY7apmfDzAI9zxvj+W7Yd07QUr9Jlf930k7OHgeV0yU+54KsbvD29fX97eZ4lTv73H68BmX+Rvfwbpmt5w0iOln9UEuRsvYxp2Ys1wKPNIuH0dRVG5dmNgC5sC56l4sgWX2V/NOd0++lA7cPti3lDWUZ5/iGaVBd4UjE5ZWzi7o83RwCubLKoaV/ZQol8fuj5qIr4IoX5CZEQEAlrF+kwrUwRCQORiH6RTJtJDhDJ0y4SNm0meV3HpDgsFru2VwpQSLdi/h0EKO0iBXcQoLSLVKVdRkco/Sp1njZdO0RhT7dknSdRXUvWDlj6JbuHlEq/SveQU+lXyb23iOUEuiUj9xax9EsGU/cLI1bnkhk3y2Dubl740q8STN4ND1/E1l20gpm84eFLv2QwrTc8fOmXDKb1F4avriVrxzL9ksG0Hih2eFdg1RWxmkoVPGff6JilLBeWJVuKi0dp+E7TP1mR8IQthTkSMlFxflSKmERh+tJq8M44Z1mtwVOazMsTnJVkQnl06EcMbVWOMtvOyzc+w6yIQjqM6SqnUchpPFyxQrR82718Ee1nSZpWYxwgbFmj8RR1FHN9t4HWtiBadFO0cC1yIlr7R2iPsiHLdMRyGnYR67BpdOGCoKd7Nl3jfBeuXXq6Zz90jfNduMzqH7qdhGWkyKduixauDXvHPT8sW2bRxXAZ29M9PyybRheuuPuw3Ek+hZFutLAy0KPtJJlysG60Z1cw+pj8vzFZv+OeXcR4BMcNvInled2gddwmWtuxAFrbVrGtnsvdw4XvgQseRt8AYXHX/AScCp8ARNrgsiSOy59RVoKbteJG4JixJZcbxBGRxzVy0ymxdvYOyLntXQ4YknMU4NDVnPLs2tMjOOV0OglIiTZOxAVyDBta8G5ok/YOWFe/n8JqFKfZ6iUR91w+HSflX3c0cCdgGhw9yP7Ex370BI7JO3EJRDCbzVAUKbywA/0P2lYJjkJ+TyE/uZq3wWrR46rvtF+QKXZX3lh+VUWHpFzef0Nw8s+aVSe+FDtlnkQDO1htjyfFt3n5CSAOh8OqZzHSfef7pg8AtuVVB151rqrM8mpcHVUtp+d6IVesn6uqitNzvZCrazm6uapKOD3XC7kS/Vxh/eZx0xs3aCWXurMbB9ZXluusAFKbsQK/4aIbB7hByvY1L7odWCzpSalIIe2kYHmkJ7Un1UoWA92kYGGjJ6Ui5dq6ScEiSE9KRYroLg47sF6yS6g/zd1O2IYNcjcfvWOiyN1il/qxc53crfXi01fsIrvp1u2q45rQ6S5p/tVdgngtUrqDl3vCxpLL/KH+qloB8obi+6Z5iaps8KDa28aFqBP2ZjyM+MbN/F9668QT8a2R31HVBg2bbIkbDCHdyvvqdO1qU+FP4BWHx3/EtjtX+3d2+Pk/</diagram></mxfile>
\ No newline at end of file
## 题目地址
https://leetcode.com/problems/combination-sum/description/
## 题目描述
```
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
```
## 思路
这道题目是求集合,并不是`求极值`,因此动态规划不是特别切合,因此我们需要考虑别的方法。
这种题目其实有一个通用的解法,就是回溯法。
网上也有大神给出了这种回溯法解题的
[通用写法](https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)),这里的所有的解法使用通用方法解答。
除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。
我们先来看下通用解法的解题思路,我画了一张图:
![backtrack](../assets/problems/backtrack.png)
通用写法的具体代码见下方代码区。
## 关键点解析
- 回溯法
- backtrack 解题公式
## 代码
```js
/*
* @lc app=leetcode id=39 lang=javascript
*
......@@ -68,3 +125,12 @@ var combinationSum = function(candidates, target) {
backtrack(list, [], candidates.sort((a, b) => a - b), target, 0);
return list;
};
```
## 相关题目
- [40.combination-sum-ii](./40.combination-sum-ii.md)
- [46.permutations](./46.permutations.md)
- [47.permutations-ii](./47.permutations-ii.md)
- [78.subsets](./78.subsets.md)
- [90.subsets-ii](./90.subsets-ii.md)
## 题目地址
https://leetcode.com/problems/combination-sum/description/
## 题目描述
```
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
```
## 思路
这道题目是求集合,并不是`求极值`,因此动态规划不是特别切合,因此我们需要考虑别的方法。
这种题目其实有一个通用的解法,就是回溯法。
网上也有大神给出了这种回溯法解题的
[通用写法](https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)),这里的所有的解法使用通用方法解答。
除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。
我们先来看下通用解法的解题思路,我画了一张图:
![backtrack](../assets/problems/backtrack.png)
通用写法的具体代码见下方代码区。
## 关键点解析
- 回溯法
- backtrack 解题公式
## 代码
```js
/*
* @lc app=leetcode id=40 lang=javascript
*
......@@ -53,6 +110,8 @@ function backtrack(list, tempList, nums, remain, start) {
if (remain < 0) return;
else if (remain === 0) return list.push([...tempList]);
for (let i = start; i < nums.length; i++) {
// 和39.combination-sum 的其中一个区别就是这道题candidates可能有重复
// 代码表示就是下面这一行
if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates
tempList.push(nums[i]);
backtrack(list, tempList, nums, remain - nums[i], i + 1); // i + 1代表不可以重复利用, i 代表数字可以重复使用
......@@ -69,3 +128,12 @@ var combinationSum2 = function(candidates, target) {
backtrack(list, [], candidates.sort((a, b) => a - b), target, 0);
return list;
};
```
## 相关题目
- [39.combination-sum](./39.combination-sum.md)
- [46.permutations](./46.permutations.md)
- [47.permutations-ii](./47.permutations-ii.md)
- [78.subsets](./78.subsets.md)
- [90.subsets-ii](./90.subsets-ii.md)
## 题目地址
https://leetcode.com/problems/combination-sum/description/
## 题目描述
```
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
```
## 思路
这道题目是求集合,并不是`求极值`,因此动态规划不是特别切合,因此我们需要考虑别的方法。
这种题目其实有一个通用的解法,就是回溯法。
网上也有大神给出了这种回溯法解题的
[通用写法](https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)),这里的所有的解法使用通用方法解答。
除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。
我们先来看下通用解法的解题思路,我画了一张图:
![backtrack](../assets/problems/backtrack.png)
通用写法的具体代码见下方代码区。
## 关键点解析
- 回溯法
- backtrack 解题公式
## 代码
```js
/*
* @lc app=leetcode id=46 lang=javascript
*
......@@ -47,4 +104,13 @@ var permute = function(nums) {
backtrack(list, [], nums)
return list
};
```
## 相关题目
- [39.combination-sum](./39.combination-sum.md)
- [40.combination-sum-ii](./40.combination-sum-ii.md)
- [47.permutations-ii](./47.permutations-ii.md)
- [78.subsets](./78.subsets.md)
- [90.subsets-ii](./90.subsets-ii.md)
## 题目地址
https://leetcode.com/problems/combination-sum/description/
## 题目描述
```
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
```
## 思路
这道题目是求集合,并不是`求极值`,因此动态规划不是特别切合,因此我们需要考虑别的方法。
这种题目其实有一个通用的解法,就是回溯法。
网上也有大神给出了这种回溯法解题的
[通用写法](https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)),这里的所有的解法使用通用方法解答。
除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。
我们先来看下通用解法的解题思路,我画了一张图:
![backtrack](../assets/problems/backtrack.png)
通用写法的具体代码见下方代码区。
## 关键点解析
- 回溯法
- backtrack 解题公式
## 代码
```js
/*
* @lc app=leetcode id=47 lang=javascript
*
......@@ -30,8 +87,10 @@
function backtrack(list, nums, tempList, visited) {
if (tempList.length === nums.length) return list.push([...tempList]);
for (let i = 0; i < nums.length; i++) {
if (visited[i]) continue;
// visited[i - 1] 容易忽略
// 和46.permutations的区别是这道题的nums是可以重复的
// 我们需要过滤这种情况
if (visited[i]) continue; // 不能用tempList.includes(nums[i])了,因为有重复
// visited[i - 1] 这个判断容易忽略
if (i > 0 && nums[i] === nums[i - 1] && visited[i - 1]) continue;
visited[i] = true;
......@@ -50,3 +109,12 @@ var permuteUnique = function(nums) {
backtrack(list, nums.sort((a, b) => a - b), [], []);
return list;
};
```
## 相关题目
- [39.combination-sum](./39.combination-sum.md)
- [40.combination-sum-ii](./40.combination-sum-ii.md)
- [46.permutations](./46.permutations.md)
- [78.subsets](./78.subsets.md)
- [90.subsets-ii](./90.subsets-ii.md)
## 题目地址
https://leetcode.com/problems/combination-sum/description/
## 题目描述
```
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
```
## 思路
这道题目是求集合,并不是`求极值`,因此动态规划不是特别切合,因此我们需要考虑别的方法。
这种题目其实有一个通用的解法,就是回溯法。
网上也有大神给出了这种回溯法解题的
[通用写法](https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)),这里的所有的解法使用通用方法解答。
除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。
我们先来看下通用解法的解题思路,我画了一张图:
![backtrack](../assets/problems/backtrack.png)
通用写法的具体代码见下方代码区。
## 关键点解析
- 回溯法
- backtrack 解题公式
## 代码
```js
/*
* @lc app=leetcode id=78 lang=javascript
*
......@@ -50,4 +109,14 @@ var subsets = function(nums) {
backtrack(list, [], nums, 0);
return list;
};
```
## 相关题目
- [39.combination-sum](./39.combination-sum.md)
- [40.combination-sum-ii](./40.combination-sum-ii.md)
- [46.permutations](./46.permutations.md)
- [47.permutations-ii](./47.permutations-ii.md)
- [90.subsets-ii](./90.subsets-ii.md)
## 题目地址
https://leetcode.com/problems/combination-sum/description/
## 题目描述
```
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
```
## 思路
这道题目是求集合,并不是`求极值`,因此动态规划不是特别切合,因此我们需要考虑别的方法。
这种题目其实有一个通用的解法,就是回溯法。
网上也有大神给出了这种回溯法解题的
[通用写法](https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)),这里的所有的解法使用通用方法解答。
除了这道题目还有很多其他题目可以用这种通用解法,具体的题目见后方相关题目部分。
我们先来看下通用解法的解题思路,我画了一张图:
![backtrack](../assets/problems/backtrack.png)
通用写法的具体代码见下方代码区。
## 关键点解析
- 回溯法
- backtrack 解题公式
## 代码
```js
/*
* @lc app=leetcode id=90 lang=javascript
*
......@@ -35,6 +95,8 @@
function backtrack(list, tempList, nums, start) {
list.push([...tempList]);
for(let i = start; i < nums.length; i++) {
// 和78.subsets的区别在于这道题nums可以有重复
// 因此需要过滤这种情况
if (i > start && nums[i] === nums[i - 1]) continue;
tempList.push(nums[i]);
backtrack(list, tempList, nums, i + 1)
......@@ -50,4 +112,16 @@ var subsetsWithDup = function(nums) {
backtrack(list, [], nums.sort((a, b) => a - b), 0, [])
return list;
};
```
## 相关题目
- [39.combination-sum](./39.combination-sum.md)
- [40.combination-sum-ii](./40.combination-sum-ii.md)
- [46.permutations](./46.permutations.md)
- [47.permutations-ii](./47.permutations-ii.md)
- [78.subsets](./78.subsets.md)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册