Skip to content

Commit a3f7794

Browse files
authored
Merge branch 'master' into patch-1
2 parents 6362364 + aab3b27 commit a3f7794

File tree

69 files changed

+2508
-263
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+2508
-263
lines changed

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ English version repo and Gitbook is on [english branch](https://github.com/labul
33
# labuladong 的算法小抄
44

55
<p align='center'>
6-
<a href="https://labuladong.gitbook.io/algo" target="_blank"><img alt="Website" src="https://img.shields.io/website?label=%E5%9C%A8%E7%BA%BF%E7%94%B5%E5%AD%90%E4%B9%A6&style=flat-square&down_color=blue&down_message=%E7%82%B9%E8%BF%99%E9%87%8C&up_color=blue&up_message=%E7%82%B9%E8%BF%99%E9%87%8C&url=https%3A%2F%2Flabuladong.gitbook.io%2Falgo&logo=Gitea"></a>
6+
<a href="https://labuladong.gitee.io/algo" target="_blank"><img alt="Website" src="https://img.shields.io/website?label=%E5%9C%A8%E7%BA%BF%E7%94%B5%E5%AD%90%E4%B9%A6&style=flat-square&down_color=blue&down_message=%E7%82%B9%E8%BF%99%E9%87%8C&up_color=blue&up_message=%E7%82%B9%E8%BF%99%E9%87%8C&url=https%3A%2F%2Flabuladong.gitee.io%2Falgo&logo=Gitea"></a>
77
<a href="https://github.com/labuladong/fucking-algorithm" target="_blank"><img alt="GitHub" src="https://img.shields.io/github/stars/labuladong/fucking-algorithm?label=Stars&style=flat-square&logo=GitHub"></a>
88
</p>
99

@@ -14,6 +14,10 @@ English version repo and Gitbook is on [english branch](https://github.com/labul
1414
<a href="https://space.bilibili.com/14089380" target="_blank"><img src="https://img.shields.io/badge/B站-@labuladong-000000.svg?style=flat-square&logo=Bilibili"></a>
1515
</p>
1616

17+
![](pictures/souyisou.png)
18+
19+
好消息,《labuladong 的算法小抄》纸质书出版啦!关注公众号查看详情👆
20+
1721
<p align='center'>
1822
<img src="https://gitee.com/labuladong/pictures/raw/master/starHistory.png" width = "600" />
1923
</p>
@@ -25,27 +29,36 @@ English version repo and Gitbook is on [english branch](https://github.com/labul
2529

2630
只想要答案的话很容易,题目评论区五花八门的答案,动不动就秀 python 一行代码解决,有那么多人点赞。问题是,你去做算法题,是去学习编程语言的奇技淫巧的,还是学习算法思维的呢?你的快乐,到底源自复制别人的一行代码通过测试,已完成题目 +1,还是源自自己通过逻辑推理和算法框架不看答案写出解法?
2731

28-
网上总有大佬喷我,说我写这玩意太基础了,根本没必要啰嗦。我只能说大家刷算法就是找工作吃饭的,不是打竞赛的,我也是一路摸爬滚打过来的,我们要的是清楚明白有所得,不是故弄玄虚无所指。不想办法做到通俗易懂,难道要上来先把《算法导论》吹上天,然后把人家都心怀敬仰地劝退?
32+
网上总有大佬喷我,说我写的东西太基础,要么说不能借助框架思维来学习算法。我只能说大家刷算法就是找工作吃饭的,不是打竞赛的,我也是一路摸爬滚打过来的,我们要的是清楚明白有所得,不是故弄玄虚无所指。
33+
34+
不想办法做到通俗易懂,难道要上来先把《算法导论》吹上天,然后把人家都心怀敬仰地劝退?
2935

3036
**做啥事情做多了,都能发现套路的,我把各种算法套路框架总结出来,相信可以帮助其他人少走弯路**。我这个纯靠自学的小童鞋,花了一年时间刷题和总结,自己写了一份算法小抄,后面有目录,这里就不废话了。
3137

3238
### 使用方法
3339

34-
1、**先给本仓库点个 star,满足一下我的虚荣心**,文章质量绝对值你一个 star。我还在继续创作,给我一点继续写文的动力,感谢。
40+
**1、先给本仓库点个 star,满足一下我的虚荣心**,文章质量绝对值你一个 star。我还在继续创作,给我一点继续写文的动力,感谢。
41+
42+
**2、建议收藏我的在线网站,每篇文章开头都有对应的力扣题目链接,可以边看文章边刷题**
43+
44+
Gitbook 地址:https://labuladong.gitbook.io/algo
45+
46+
GitBook 在国内访问速度很慢,且常被攻击,我特意部署了两个镜像站点,大家可根据网络情况自行选择:
47+
48+
GitHub Pages 地址:https://labuladong.github.io/algo
3549

36-
2、**建议收藏我的 Gitbook 网站,每篇文章开头都有对应的力扣题目链接,可以边看文章边刷题**
50+
Gitee Pages 地址:https://labuladong.gitee.io/algo
3751

38-
Gitbook 地址:https://labuladong.gitbook.io/algo/
3952

40-
3、建议关注我的公众号 **labuladong**,坚持高质量原创,说是最良心最硬核的技术公众号都不为过。本仓库的文章就是从公众号里整理出来的**一部分**内容,公众号后台回复关键词【电子书】可以获得这份小抄的完整版本;回复【加群】可以加入我们的刷题群,和大家一起讨论算法问题,分享内推机会:
53+
**3、建议关注我的公众号 labuladong,坚持高质量原创,说是最良心最硬核的技术公众号都不为过**。本仓库的文章就是从公众号里整理出来的**一部分**内容,公众号可以查看更多内容;公众号后台回复关键词【加群】可以加入我们的刷题群,和大家一起讨论算法问题,分享内推机会:
4154

4255
<p align='center'>
4356
<img src="https://gitee.com/labuladong/pictures/raw/master/qrcode.jpg" width = "200" />
4457
</p>
4558

46-
4、欢迎关注 [我的知乎](https://www.zhihu.com/people/labuladong)
59+
**4、欢迎关注 [我的知乎](https://www.zhihu.com/people/labuladong)**
4760

48-
我一直在写优质文章,但是后续的文章只发布到公众号/gitbook/知乎,不能开放到 GitHub。因为本仓库太火了,很多人直接拿我的文章去开付费专栏,价格还不便宜,我这免费写给您看,何必掏冤枉钱呢?所以多多关注本作者,多多宣传,谁也不希望劣币驱逐良币不是么?
61+
我一直在写优质文章,但是后续的文章只发布到公众号/网站/知乎,不能开放到 GitHub。因为本仓库太火了,很多人直接拿我的文章去开付费专栏,价格还不便宜,我这免费写给您看,何必掏冤枉钱呢?所以多多关注本作者,多多宣传,谁也不希望劣币驱逐良币不是么?
4962

5063
其他的先不多说了,直接上干货吧,我们一起搞定 LeetCode,感受一下支配算法的乐趣。
5164

pictures/qrcode.jpg

9.41 KB
Loading

出版推广1.jpeg

120 KB
Loading

动态规划系列/动态规划之KMP字符匹配算法.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
![](../pictures/souyisou.png)
1212

1313
相关推荐:
14-
* [经典动态规划:最长公共子序列](https://labuladong.gitbook.io/algo)
15-
* [特殊数据结构:单调栈](https://labuladong.gitbook.io/algo)
14+
* [经典动态规划:最长公共子序列](https://labuladong.gitbook.io/algo/)
15+
* [特殊数据结构:单调栈](https://labuladong.gitbook.io/algo/)
1616

1717
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
1818

@@ -423,12 +423,48 @@ KMP 算法也就是动态规划那点事,我们的公众号文章目录有一
423423

424424
**_____________**
425425

426-
**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**
426+
**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**
427427

428428
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**
429429

430430
<p align='center'>
431431
<img src="../pictures/qrcode.jpg" width=200 >
432432
</p>
433433

434-
======其他语言代码======
434+
======其他语言代码======
435+
[MoguCloud](https://github.com/MoguCloud) 提供 实现 strStr() 的 Python 完整代码:
436+
```py
437+
class Solution:
438+
def strStr(self, haystack: str, needle: str) -> int:
439+
# 边界条件判断
440+
if not needle:
441+
return 0
442+
pat = needle
443+
txt = haystack
444+
445+
M = len(pat)
446+
# dp[状态][字符] = 下个状态
447+
dp = [[0 for _ in range(256)] for _ in pat]
448+
# base case
449+
dp[0][ord(pat[0])] = 1
450+
# 影子状态 X 初始化为 0
451+
X = 0
452+
for j in range(1, M):
453+
for c in range(256):
454+
dp[j][c] = dp[X][c]
455+
dp[j][ord(pat[j])] = j + 1
456+
# 更新影子状态
457+
X = dp[X][ord(pat[j])]
458+
459+
N = len(txt)
460+
# pat 初始状态为 0
461+
j = 0
462+
for i in range(N):
463+
# 计算 pat 的下一个状态
464+
j = dp[j][ord(txt[i])]
465+
# 到达终止态,返回结果
466+
if j == M:
467+
return i - M + 1
468+
# 没到达终止态,匹配失败
469+
return -1
470+
```

动态规划系列/动态规划之博弈问题.md

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
![](../pictures/souyisou.png)
1212

1313
相关推荐:
14-
* [40张图解:TCP三次握手和四次挥手面试题](https://labuladong.gitbook.io/algo)
15-
* [如何计算完全二叉树的节点数](https://labuladong.gitbook.io/algo)
14+
* [40张图解:TCP三次握手和四次挥手面试题](https://labuladong.gitbook.io/algo/)
15+
* [如何计算完全二叉树的节点数](https://labuladong.gitbook.io/algo/)
1616

1717
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
1818

1919
[877.石子游戏](https://leetcode-cn.com/problems/stone-game)
2020

2121
**-----------**
2222

23-
上一篇文章 [几道智力题](https://labuladong.gitbook.io/algo) 中讨论到一个有趣的「石头游戏」,通过题目的限制条件,这个游戏是先手必胜的。但是智力题终究是智力题,真正的算法问题肯定不会是投机取巧能搞定的。所以,本文就借石头游戏来讲讲「假设两个人都足够聪明,最后谁会获胜」这一类问题该如何用动态规划算法解决。
23+
上一篇文章 [几道智力题](https://labuladong.gitbook.io/algo/) 中讨论到一个有趣的「石头游戏」,通过题目的限制条件,这个游戏是先手必胜的。但是智力题终究是智力题,真正的算法问题肯定不会是投机取巧能搞定的。所以,本文就借石头游戏来讲讲「假设两个人都足够聪明,最后谁会获胜」这一类问题该如何用动态规划算法解决。
2424

2525
博弈类问题的套路都差不多,下文参考 [这个 YouTube 视频](https://www.youtube.com/watch?v=WxpIHvsu1RI) 的思路讲解,其核心思路是在二维 dp 的基础上使用元组分别存储两个人的博弈结果。掌握了这个技巧以后,别人再问你什么俩海盗分宝石,俩人拿硬币的问题,你就告诉别人:我懒得想,直接给你写个算法算一下得了。
2626

@@ -207,7 +207,7 @@ int stoneGame(int[] piles) {
207207

208208
**_____________**
209209

210-
**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**
210+
**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**
211211

212212
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**
213213

@@ -221,7 +221,7 @@ int stoneGame(int[] piles) {
221221

222222

223223

224-
python3版本
224+
* python3版本
225225

226226
由[SCUHZS](https://github.com/brucecat)提供
227227

@@ -287,3 +287,44 @@ class Solution:
287287

288288
```
289289

290+
* C++ 版本
291+
292+
由 [TCeason](https://github.com/TCeason) 提供
293+
294+
这里采用 hash map 来解决问题
295+
296+
```cpp
297+
class Solution {
298+
public:
299+
unordered_map<int, int> memo;
300+
301+
int dfs(vector<int> &piles, int index) {
302+
// 从两边向中间获取
303+
// index 值为 1/2 piles.size() 时可以停止算法
304+
if (index == piles.size() / 2)
305+
return 0;
306+
307+
// 减少计算,快速返回已有结果
308+
if (memo.count(index))
309+
return memo[index];
310+
311+
// 防止第一次取最右时越界
312+
int n = piles.size() - 1;
313+
314+
// 先手选择最左边或最右边后的分数
315+
int l = piles[index] + dfs(piles, index + 1);
316+
int r = piles[n - index] + dfs(piles, index + 1);
317+
318+
// 返回先手左或右边的最高分
319+
return memo[index] = max(l, r);
320+
}
321+
322+
bool stoneGame(vector<int>& piles) {
323+
// 最佳发挥时:
324+
// 先手得分 * 2 > 总大小 则先手者胜利
325+
return dfs(piles, 0) * 2 > accumulate(begin(piles), end(piles), 0);
326+
}
327+
};
328+
329+
```
330+

动态规划系列/动态规划之四键键盘.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
![](../pictures/souyisou.png)
1212

1313
相关推荐:
14-
* [如何高效寻找素数](https://labuladong.gitbook.io/algo)
15-
* [动态规划解题套路框架](https://labuladong.gitbook.io/algo)
14+
* [如何高效寻找素数](https://labuladong.gitbook.io/algo/)
15+
* [动态规划解题套路框架](https://labuladong.gitbook.io/algo/)
1616

1717
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
1818

@@ -192,7 +192,7 @@ def dp(n, a_num, copy):
192192

193193
**_____________**
194194

195-
**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**
195+
**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**
196196

197197
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**
198198

动态规划系列/动态规划之正则表达.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
![](../pictures/souyisou.png)
1111

1212
相关推荐:
13-
* [我写了首诗,把滑动窗口算法算法变成了默写题](https://labuladong.gitbook.io/algo)
14-
* [二分查找高效判定子序列](https://labuladong.gitbook.io/algo)
13+
* [我写了首诗,把滑动窗口算法算法变成了默写题](https://labuladong.gitbook.io/algo/)
14+
* [二分查找高效判定子序列](https://labuladong.gitbook.io/algo/)
1515

1616
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
1717

@@ -287,7 +287,7 @@ bool dp(string& s, int i, string& p, int j) {
287287

288288
**_____________**
289289

290-
**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**
290+
**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**
291291

292292
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**
293293

动态规划系列/动态规划设计:最长递增子序列.md

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
![](../pictures/souyisou.png)
1111

1212
相关推荐:
13-
* [动态规划设计:最大子数组](https://labuladong.gitbook.io/algo)
14-
* [一文学会递归解题](https://labuladong.gitbook.io/algo)
13+
* [动态规划设计:最大子数组](https://labuladong.gitbook.io/algo/)
14+
* [一文学会递归解题](https://labuladong.gitbook.io/algo/)
1515

1616
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
1717

1818
[300.最长上升子序列](https://leetcode-cn.com/problems/longest-increasing-subsequence)
1919

2020
**-----------**
2121

22-
也许有读者看了前文 [动态规划详解](https://labuladong.gitbook.io/algo),学会了动态规划的套路:找到了问题的「状态」,明确了 `dp` 数组/函数的含义,定义了 base case;但是不知道如何确定「选择」,也就是不到状态转移的关系,依然写不出动态规划解法,怎么办?
22+
也许有读者看了前文 [动态规划详解](https://labuladong.gitbook.io/algo/),学会了动态规划的套路:找到了问题的「状态」,明确了 `dp` 数组/函数的含义,定义了 base case;但是不知道如何确定「选择」,也就是不到状态转移的关系,依然写不出动态规划解法,怎么办?
2323

2424
不要担心,动态规划的难点本来就在于寻找正确的状态转移方程,本文就借助经典的「最长递增子序列问题」来讲一讲设计动态规划的通用技巧:**数学归纳思想**
2525

@@ -43,7 +43,7 @@
4343

4444
**我们的定义是这样的:`dp[i]` 表示以 `nums[i]` 这个数结尾的最长递增子序列的长度。**
4545

46-
PS:为什么这样定义呢?这是解决子序列问题的一个套路,后文[动态规划之子序列问题解题模板](https://labuladong.gitbook.io/algo) 总结了几种常见套路。你读完本章所有的动态规划问题,就会发现 `dp` 数组的定义方法也就那几种。
46+
PS:为什么这样定义呢?这是解决子序列问题的一个套路,后文[动态规划之子序列问题解题模板](https://labuladong.gitbook.io/algo/) 总结了几种常见套路。你读完本章所有的动态规划问题,就会发现 `dp` 数组的定义方法也就那几种。
4747

4848
根据这个定义,我们就可以推出 base case:`dp[i]` 初始值为 1,因为以 `nums[i]` 结尾的最长递增子序列起码要包含它自己。
4949

@@ -164,7 +164,7 @@ public int lengthOfLIS(int[] nums) {
164164

165165
我们只要把处理扑克牌的过程编程写出来即可。每次处理一张扑克牌不是要找一个合适的牌堆顶来放吗,牌堆顶的牌不是**有序**吗,这就能用到二分查找了:用二分查找来搜索当前牌应放置的位置。
166166

167-
PS:旧文[二分查找算法详解](https://labuladong.gitbook.io/algo)详细介绍了二分查找的细节及变体,这里就完美应用上了,如果没读过强烈建议阅读。
167+
PS:旧文[二分查找算法详解](https://labuladong.gitbook.io/algo/)详细介绍了二分查找的细节及变体,这里就完美应用上了,如果没读过强烈建议阅读。
168168

169169
```java
170170
public int lengthOfLIS(int[] nums) {
@@ -207,7 +207,7 @@ public int lengthOfLIS(int[] nums) {
207207

208208
**_____________**
209209

210-
**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**
210+
**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**
211211

212212
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**
213213

@@ -217,6 +217,51 @@ public int lengthOfLIS(int[] nums) {
217217

218218
======其他语言代码======
219219

220+
```python 动态规划
221+
class Solution:
222+
def lengthOfLIS(self, nums: List[int]) -> int:
223+
n = len(nums)
224+
f = [1] * (n)
225+
226+
for i in range(n):
227+
for j in range(i):
228+
if nums[j] < nums[i]:
229+
f[i] = max(f[i], f[j] + 1)
230+
231+
res = 0
232+
for i in range(n):
233+
res = max(res, f[i])
234+
return res
235+
```
236+
237+
```python 二分查找
238+
class Solution:
239+
def lengthOfLIS(self, nums: List[int]) -> int:
240+
stack = []
241+
242+
def find_index(num):
243+
l, r = 0, len(stack)
244+
while l < r:
245+
mid = l + r >> 1
246+
if stack[mid] >= num:
247+
r = mid
248+
else:
249+
l = mid + 1
250+
251+
return r
252+
253+
254+
for num in nums:
255+
if not stack or num > stack[-1]:
256+
stack.append(num)
257+
else:
258+
position = find_index(num)
259+
stack[position] = num
260+
261+
return len(stack)
262+
```
263+
264+
220265
[Kian](https://github.com/KianKw/) 提供 C++ 代码
221266

222267
```c++
@@ -257,3 +302,4 @@ public:
257302
}
258303
};
259304
```
305+

0 commit comments

Comments
 (0)