mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update
This commit is contained in:
96
problems/0139.单词拆分.md
Normal file
96
problems/0139.单词拆分.md
Normal file
@ -0,0 +1,96 @@
|
||||
|
||||
// 如何往 完全背包上靠?
|
||||
// 用多次倒是可以往 完全背包上靠一靠
|
||||
// 和单词分割的问题有点像
|
||||
|
||||
[回溯算法:分割回文串](https://mp.weixin.qq.com/s/Pb1epUTbU8fHIht-g_MS5Q)
|
||||
|
||||
回溯法代码:
|
||||
```
|
||||
class Solution {
|
||||
private:
|
||||
bool backtracking (const string& s, const unordered_set<string>& wordSet, int startIndex) {
|
||||
if (startIndex >= s.size()) {
|
||||
return true;
|
||||
}
|
||||
for (int i = startIndex; i < s.size(); i++) {
|
||||
string word = s.substr(startIndex, i - startIndex + 1);
|
||||
if (wordSet.find(word) != wordSet.end() && backtracking(s, wordSet, i + 1)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public:
|
||||
bool wordBreak(string s, vector<string>& wordDict) {
|
||||
unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
|
||||
return backtracking(s, wordSet, 0);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
```
|
||||
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"
|
||||
["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]
|
||||
```
|
||||
|
||||
可以使用一个一维数组保存一下,递归过程中计算的结果,C++代码如下:
|
||||
|
||||
使用memory数组保存 每次计算的以startIndex起始的计算结果,如果memory[startIndex]里已经被赋值了,直接用memory[startIndex]的结果。
|
||||
```
|
||||
class Solution {
|
||||
private:
|
||||
bool backtracking (const string& s,
|
||||
const unordered_set<string>& wordSet,
|
||||
vector<int>& memory,
|
||||
int startIndex) {
|
||||
if (startIndex >= s.size()) {
|
||||
return true;
|
||||
}
|
||||
// 如果memory[startIndex]不是初始值了,直接使用memory[startIndex]的结果
|
||||
if (memory[startIndex] != -1) return memory[startIndex];
|
||||
for (int i = startIndex; i < s.size(); i++) {
|
||||
string word = s.substr(startIndex, i - startIndex + 1);
|
||||
if (wordSet.find(word) != wordSet.end() && backtracking(s, wordSet, memory, i + 1)) {
|
||||
memory[startIndex] = 1; // 记录以startIndex开始的子串是可以被拆分的
|
||||
return true;
|
||||
}
|
||||
}
|
||||
memory[startIndex] = 0; // 记录以startIndex开始的子串是不可以被拆分的
|
||||
return false;
|
||||
}
|
||||
public:
|
||||
bool wordBreak(string s, vector<string>& wordDict) {
|
||||
unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
|
||||
vector<int> memory(s.size(), -1); // -1 表示初始化状态
|
||||
return backtracking(s, wordSet, memory, 0);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
得好好分析一下,完全背包和01背包,这个对于刷leetcode太重要了
|
||||
|
||||
注意这里要空出一个 dp[0] 来做起始位置
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
bool wordBreak(string s, vector<string>& wordDict) {
|
||||
unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
|
||||
vector<bool> dp(s.size() + 1, false);
|
||||
dp[0] = true;
|
||||
for (int i = 1; i <= s.size(); i++) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
string word = s.substr(j, i - j); //substr(起始位置,截取的个数)
|
||||
if (wordSet.find(word) != wordSet.end() && dp[j]) {
|
||||
dp[i] = true;
|
||||
}
|
||||
}
|
||||
//for (int k = 0; k <=i; k++) cout << dp[k] << " ";
|
||||
//cout << endl;
|
||||
}
|
||||
return dp[s.size()];
|
||||
}
|
||||
};
|
||||
```
|
||||
时间复杂度起始是O(n^3),因为substr返回子串的副本是O(n)的复杂度(n是substring的长度)
|
57
problems/0860.柠檬水找零.md
Normal file
57
problems/0860.柠檬水找零.md
Normal file
@ -0,0 +1,57 @@
|
||||
> 如果对贪心算法理论基础还不了解的话,可以看看这篇:[关于贪心算法,你该了解这些!](https://mp.weixin.qq.com/s/O935TaoHE9Eexwe_vSbRAg) ,相信看完之后对贪心就有基本的了解了。
|
||||
|
||||
这道题目我们需要维护三种金额的数量,5,10和20。
|
||||
|
||||
有如下三种情况:
|
||||
* 情况一:账单是5,直接收下。
|
||||
* 情况二:账单是10,消耗一个5,增加一个10
|
||||
* 情况三:账单是20,优先消耗一个10和一个5,如果不够,在消耗三个5
|
||||
|
||||
这道题大家可能感觉纯模拟就可以了,其实这里有贪心,就在情况三中。
|
||||
|
||||
账单是20的情况,为什么要优先消耗一个10和一个5呢?
|
||||
|
||||
**因为美元10只能给账单20找零,而美元5可以给账单10和账单20找零,美元5更万能!**
|
||||
|
||||
局部最优:遇到账单20,优先消耗美元10,完成本次找零。全局最优:完成全部账单的找零。
|
||||
|
||||
局部最优可以推出全局最优,并找不出反例,那么就试试贪心。
|
||||
|
||||
C++代码如下:
|
||||
|
||||
```C++
|
||||
class Solution {
|
||||
public:
|
||||
bool lemonadeChange(vector<int>& bills) {
|
||||
int five = 0, ten = 0, twenty = 0;
|
||||
for (int bill : bills) {
|
||||
// 情况一
|
||||
if (bill == 5) five++;
|
||||
// 情况二
|
||||
if (bill == 10) {
|
||||
if (five <= 0) return false;
|
||||
ten++;
|
||||
five--;
|
||||
}
|
||||
// 情况三
|
||||
if (bill == 20) {
|
||||
// 优先消耗10美元,因为5美元的找零用处更大,能多留着就多留着
|
||||
if (five > 0 && ten > 0) {
|
||||
five--;
|
||||
ten--;
|
||||
twenty++; // 其实这行代码可以删了,因为记录20已经没有意义了,不会用20来找零
|
||||
} else if (five >= 3) {
|
||||
five -= 3;
|
||||
twenty++; // 同理,这行代码也可以删了
|
||||
} else return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
```
|
||||
> **我是[程序员Carl](https://github.com/youngyangyang04),可以找我[组队刷题](https://img-blog.csdnimg.cn/20201115103410182.png),也可以在[B站上找到我](https://space.bilibili.com/525438321),本文[leetcode刷题攻略](https://github.com/youngyangyang04/leetcode-master)已收录,更多[精彩算法文章](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzUxNjY5NTYxNA==&action=getalbum&album_id=1485825793120387074&scene=173#wechat_redirect)尽在公众号:[代码随想录](https://img-blog.csdnimg.cn/20201124161234338.png),关注后就会发现和「代码随想录」相见恨晚!**
|
||||
|
||||
**如果感觉题解对你有帮助,不要吝啬给一个👍吧!**
|
||||
|
@ -12,10 +12,10 @@
|
||||
|
||||
# 文章篇
|
||||
|
||||
* 代码风格
|
||||
* 编程素养
|
||||
* [看了这么多代码,谈一谈代码风格!](https://mp.weixin.qq.com/s/UR9ztxz3AyL3qdHn_zMbqw)
|
||||
|
||||
* 求职
|
||||
* 求职
|
||||
* [程序员的简历应该这么写!!(附简历模板)](https://mp.weixin.qq.com/s/nCTUzuRTBo1_R_xagVszsA)
|
||||
* [BAT级别技术面试流程和注意事项都在这里了](https://mp.weixin.qq.com/s/815qCyFGVIxwut9I_7PNFw)
|
||||
* [深圳原来有这么多互联网公司,你都知道么?](https://mp.weixin.qq.com/s/Yzrkim-5bY0Df66Ao-hoqA)
|
||||
@ -26,6 +26,7 @@
|
||||
* 算法性能分析
|
||||
* [关于时间复杂度,你不知道的都在这里!](https://mp.weixin.qq.com/s/LWBfehW1gMuEnXtQjJo-sw)
|
||||
* [O(n)的算法居然超时了,此时的n究竟是多大?](https://mp.weixin.qq.com/s/73ryNsuPFvBQkt6BbhNzLA)
|
||||
* [通过一道面试题目,讲一讲递归算法的时间复杂度!](https://mp.weixin.qq.com/s/I6ZXFbw09NR31F5CJR_geQ)
|
||||
|
||||
* 数组
|
||||
* [必须掌握的数组理论知识](https://mp.weixin.qq.com/s/X7R55wSENyY62le0Fiawsg)
|
||||
@ -172,10 +173,10 @@
|
||||
# 视频篇
|
||||
|
||||
* 算法
|
||||
* [带你学透KMP算法(理论篇&代码篇)](https://mp.weixin.qq.com/s/SFAs4tbo2jDgzST9AsF2xg)
|
||||
* [带你学透回溯算法(理论篇)](https://mp.weixin.qq.com/s/wDd5azGIYWjbU0fdua_qBg)
|
||||
* [回溯算法:组合问题](https://mp.weixin.qq.com/s/a_r5JR93K_rBKSFplPGNAA)
|
||||
* [回溯算法:组合问题的剪枝操作](https://mp.weixin.qq.com/s/CK0kj9lq8-rFajxL4amyEg)
|
||||
* [带你学透KMP算法(理论篇&代码篇)](https://mp.weixin.qq.com/s/SFAs4tbo2jDgzST9AsF2xg)
|
||||
* C++
|
||||
* 听说C++ primer 太厚了 看不进去?:https://www.bilibili.com/video/BV1Z5411874t
|
||||
* C++ primer 第一章,你要知道的知识点还有这些!:https://www.bilibili.com/video/BV1Kv41117Ya
|
||||
|
Reference in New Issue
Block a user