-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
1,216 additions
and
871 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# 第七章 回溯算法part03 | ||
|
||
详细布置 | ||
|
||
## 39. 组合总和 | ||
|
||
本题是 集合里元素可以用无数次,那么和组合问题的差别 其实仅在于 startIndex上的控制 | ||
|
||
题目链接/文章讲解:https://programmercarl.com/0039.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C.html | ||
视频讲解:https://www.bilibili.com/video/BV1KT4y1M7HJ | ||
|
||
## 40.组合总和II | ||
|
||
本题开始涉及到一个问题了:去重。 | ||
|
||
注意题目中给我们 集合是有重复元素的,那么求出来的 组合有可能重复,但题目要求不能有重复组合。 | ||
|
||
题目链接/文章讲解:https://programmercarl.com/0040.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8CII.html | ||
视频讲解:https://www.bilibili.com/video/BV12V4y1V73A | ||
|
||
## 131.分割回文串 | ||
|
||
本题较难,大家先看视频来理解 分割问题,明天还会有一道分割问题,先打打基础。 | ||
|
||
https://programmercarl.com/0131.%E5%88%86%E5%89%B2%E5%9B%9E%E6%96%87%E4%B8%B2.html | ||
视频讲解:https://www.bilibili.com/video/BV1c54y1e7k6 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# 131.分割回文串 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
string s;vector<vector<string>>res;vector<string>cur; | ||
bool valid(int l,int r){ | ||
int lptr=l,rptr=r;while(lptr<=r){if(s[lptr]!=s[rptr])return false;lptr++;rptr--;}return true; | ||
} | ||
void bt(int start){ | ||
if(start==s.size()){res.push_back(cur);return;} | ||
for(int i=start;i<s.size();i++){ | ||
if(valid(start,i)){ | ||
cur.push_back(s.substr(start,i-start+1)); | ||
bt(i+1); | ||
cur.pop_back(); | ||
} | ||
} | ||
} | ||
vector<vector<string>> partition(string s) { | ||
this->s=s;bt(0);return res; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 39. 组合总和 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
vector<vector<int>>res; | ||
vector<int>cur;vector<int>candidates; | ||
int s=0,t; | ||
void bt(int start){ | ||
if(s>=t){if(s==t)res.push_back(cur);return;} | ||
for(int j=start;j<candidates.size();j++){int i=candidates[j]; | ||
cur.push_back(i);s+=i; | ||
bt(j); | ||
cur.pop_back();s-=i; | ||
} | ||
} | ||
vector<vector<int>> combinationSum(vector<int>& candidates, int target) { | ||
t=target;this->candidates=candidates;bt(0);return res; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# 40.组合总和II | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
vector<vector<int>>res; | ||
vector<int>cur;vector<int>candidates; | ||
int s=0,t; | ||
void bt(int start){ | ||
if(s>=t){if(s==t)res.push_back(cur);return;} | ||
for(int j=start;j<candidates.size();j++){ | ||
int i=candidates[j]; | ||
|
||
if(j>start&&candidates[j]==candidates[j-1])continue; | ||
cur.push_back(i);s+=i; | ||
bt(j+1); | ||
cur.pop_back();s-=i; | ||
} | ||
} | ||
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { | ||
t=target;sort(candidates.begin(),candidates.end());this->candidates=candidates;bt(0);return res; | ||
} | ||
}; | ||
``` | ||
`if(j>start&&candidates[j]==candidates[j-1])continue;`这而想了很久,一直以为是j大于0 | ||
为了结果不重复,所以剪枝是必须要进行的操作 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# 28 第七章 回溯算法 | ||
● 93.复原IP地址 | ||
● 78.子集 | ||
● 90.子集II | ||
|
||
详细布置 | ||
|
||
## 93.复原IP地址 | ||
|
||
本期本来是很有难度的,不过 大家做完 分割回文串 之后,本题就容易很多了 | ||
|
||
题目链接/文章讲解:https://programmercarl.com/0093.%E5%A4%8D%E5%8E%9FIP%E5%9C%B0%E5%9D%80.html | ||
视频讲解:https://www.bilibili.com/video/BV1XP4y1U73i/ | ||
|
||
## 78.子集 | ||
|
||
子集问题,就是收集树形结构中,每一个节点的结果。 整体代码其实和 回溯模板都是差不多的。 | ||
|
||
题目链接/文章讲解:https://programmercarl.com/0078.%E5%AD%90%E9%9B%86.html | ||
视频讲解:https://www.bilibili.com/video/BV1U84y1q7Ci | ||
## 90.子集II | ||
|
||
大家之前做了 40.组合总和II 和 78.子集 ,本题就是这两道题目的结合,建议自己独立做一做,本题涉及的知识,之前都讲过,没有新内容。 | ||
|
||
题目链接/文章讲解:https://programmercarl.com/0090.%E5%AD%90%E9%9B%86II.html | ||
视频讲解:https://www.bilibili.com/video/BV1vm4y1F71J |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
|
||
|
||
# 78.子集 | ||
|
||
```cpp | ||
class Solution { | ||
public:vector<vector<int>>res;vector<int>cur;vector<int>v;void bt(int start){res.push_back(cur); | ||
if(start>=v.size())return;for(int i=start;i<v.size();i++){ | ||
cur.push_back(v[i]);bt(i+1);cur.pop_back(); | ||
} | ||
} | ||
vector<vector<int>> subsets(vector<int>& nums) { | ||
v=nums;bt(0);return res; | ||
} | ||
}; | ||
``` | ||
如果把 子集问题、组合问题、分割问题都抽象为一棵树的话,那么**组合问题和分割问题都是收集树的叶子节点,而子集问题是找树的所有节点**! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
# 90.子集II | ||
|
||
```cpp | ||
class Solution { | ||
public:vector<vector<int>>res;vector<int>cur;vector<int>v;void bt(int start){res.push_back(cur); | ||
if(start==v.size())return;for(int i=start;i<v.size();i++){ | ||
if(i>start&&v[i]==v[i-1])continue; | ||
cur.push_back(v[i]);bt(i+1);cur.pop_back(); | ||
} | ||
} | ||
vector<vector<int>> subsetsWithDup(vector<int>& nums) { | ||
sort(nums.begin(),nums.end());v=nums;bt(0);return res; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
# 93.复原IP地址 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
vector<string>res;string cur;string s;void bt(int start,int cnt){ | ||
if(cnt==4||start>=s.size()){if(cnt==4&&start==s.size())res.push_back(string(cur.begin(),cur.end()-1));return;} | ||
for(int i=1;i<=3;i++){ | ||
string sub=string(s.begin()+start,s.begin()+start+i); | ||
if(valid(sub)){ | ||
auto l=cur.size(); | ||
cur+=sub+"."; | ||
bt(start+i,cnt+1); | ||
cur.erase(l); | ||
} | ||
} | ||
} | ||
bool valid(string s){ | ||
if(s.size()==0)return false; | ||
if(s[0]=='0')return s.size()==1; | ||
int a=stoi(s);return a>=0 && a<=255; | ||
} | ||
vector<string> restoreIpAddresses(string s) {this->s=s; | ||
bt(0,0);return res; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# 第七章 回溯算法part05 | ||
|
||
* 491.递增子序列 | ||
* 46.全排列 | ||
* 47.全排列 II | ||
|
||
详细布置 | ||
|
||
## 491.递增子序列 | ||
|
||
本题和大家刚做过的 90.子集II 非常像,但又很不一样,很容易掉坑里。 | ||
https://programmercarl.com/0491.%E9%80%92%E5%A2%9E%E5%AD%90%E5%BA%8F%E5%88%97.html | ||
|
||
视频讲解:https://www.bilibili.com/video/BV1EG4y1h78v | ||
|
||
|
||
## 46.全排列 | ||
本题重点感受一下,排列问题 与 组合问题,组合总和,子集问题的区别。 为什么排列问题不用 startIndex | ||
https://programmercarl.com/0046.%E5%85%A8%E6%8E%92%E5%88%97.html | ||
视频讲解:https://www.bilibili.com/video/BV19v4y1S79W | ||
|
||
## 47.全排列 II | ||
本题 就是我们讲过的 40.组合总和II 去重逻辑 和 46.全排列 的结合,可以先自己做一下,然后重点看一下 文章中 我讲的拓展内容。 used[i - 1] == true 也行,used[i - 1] == false 也行 | ||
|
||
https://programmercarl.com/0047.%E5%85%A8%E6%8E%92%E5%88%97II.html | ||
|
||
视频讲解:https://www.bilibili.com/video/BV1R84y1i7Tm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
# 46.全排列 | ||
|
||
|
||
https://programmercarl.com/0046.%E5%85%A8%E6%8E%92%E5%88%97.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE | ||
```cpp | ||
class Solution { | ||
public: | ||
vector<int>used; | ||
vector<int>cur; | ||
vector<int>v; | ||
vector<vector<int>>res; | ||
void bt() { | ||
if(cur.size()==v.size()) | ||
{res.push_back(cur);return;} | ||
for(int i=0;i<v.size();i++){ | ||
if(used[i]==0){ | ||
used[i]=1;cur.push_back(v[i]);bt();cur.pop_back();used[i]=0; | ||
} | ||
} | ||
} | ||
vector<vector<int>> permute(vector<int>& nums) { | ||
v=nums;used=vector<int>(v.size(),0);bt();return res; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 47.全排列 II | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
vector<int>used; | ||
vector<int>cur; | ||
vector<int>v; | ||
vector<vector<int>>res; | ||
void bt() { | ||
if(cur.size()==v.size()){ | ||
res.push_back(cur);return; | ||
} | ||
for(int i=0;i<v.size();i++){ | ||
if(used[i]==1)continue; | ||
if(i>0&&v[i]==v[i-1]&&used[i-1]==0)continue; | ||
used[i]=1; | ||
cur.push_back(v[i]); | ||
bt(); | ||
cur.pop_back(); | ||
used[i]=0; | ||
} | ||
} | ||
vector<vector<int>> permuteUnique(vector<int>& nums) { | ||
v=nums;sort(v.begin(),v.end()); | ||
used=vector<int>(v.size(),0); | ||
bt(); | ||
return res; | ||
} | ||
}; | ||
``` | ||
如何剪枝同一层使用过的:`&&used[i-1]==0`,一下子想不到。 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
# 491.递增子序列 | ||
|
||
```cpp | ||
class Solution { | ||
public:vector<vector<int>>res; | ||
vector<int>cur; | ||
vector<int>v; | ||
void bt(int start){ | ||
if(cur.size()>1)res.push_back(cur); | ||
if(start>=v.size())return; | ||
unordered_set<int> uset; | ||
for(int i=start;i<v.size();i++){ | ||
if(cur.empty()||v[i]>=cur.back()) | ||
{ | ||
if(uset.find(v[i])!=uset.end())continue; | ||
uset.insert(v[i]); | ||
cur.push_back(v[i]);bt(i+1);cur.pop_back(); | ||
} | ||
} | ||
|
||
} | ||
vector<vector<int>> findSubsequences(vector<int>& nums) {v=nums;bt(0);return res;} | ||
}; | ||
|
||
``` | ||
本层访问过的元素不再访问,误以为是前后不用重复的就行,需要使用set |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 第七章 回溯算法part06 | ||
● 332.重新安排行程 | ||
● 51. N皇后 | ||
● 37. 解数独 | ||
● 总结 | ||
|
||
详细布置 | ||
|
||
今天这三道题都非常难,那么这么难的题,为啥一天做三道? | ||
|
||
因为 一刷 也不求大家能把这么难的问题解决,所以 大家一刷的时候,就了解一下题目的要求,了解一下解题思路,不求能直接写出代码,先大概熟悉一下这些题,二刷的时候,随着对回溯算法的深入理解,再去解决如下三题。 | ||
|
||
大家今天的任务,其实是 对回溯算法章节做一个总结就行。 | ||
|
||
重点是看 回溯算法总结篇: | ||
https://programmercarl.com/%E5%9B%9E%E6%BA%AF%E6%80%BB%E7%BB%93.html | ||
|
||
## 332.重新安排行程(可跳过) | ||
https://programmercarl.com/0332.%E9%87%8D%E6%96%B0%E5%AE%89%E6%8E%92%E8%A1%8C%E7%A8%8B.html | ||
|
||
## 51. N皇后(可跳过) | ||
https://programmercarl.com/0051.N%E7%9A%87%E5%90%8E.html | ||
视频讲解:https://www.bilibili.com/video/BV1Rd4y1c7Bq | ||
|
||
## 37. 解数独(可跳过) | ||
https://programmercarl.com/0037.%E8%A7%A3%E6%95%B0%E7%8B%AC.html | ||
视频讲解:https://www.bilibili.com/video/BV1TW4y1471V | ||
|
||
总结 | ||
https://programmercarl.com/%E5%9B%9E%E6%BA%AF%E6%80%BB%E7%BB%93.html |
Oops, something went wrong.