diff --git a/README.md b/README.md index 45995ef..0a74a82 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ English version repo and Gitbook is on [english branch](https://github.com/labul # labuladong 的算法小抄
@@ -16,6 +16,8 @@ English version repo and Gitbook is on [english branch](https://github.com/labul  +好消息,《labuladong 的算法小抄》纸质书出版啦!关注公众号查看详情👆 +
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+[MoguCloud](https://github.com/MoguCloud) 提供 实现 strStr() 的 Python 完整代码:
+```py
+class Solution:
+ def strStr(self, haystack: str, needle: str) -> int:
+ # 边界条件判断
+ if not needle:
+ return 0
+ pat = needle
+ txt = haystack
+
+ M = len(pat)
+ # dp[状态][字符] = 下个状态
+ dp = [[0 for _ in range(256)] for _ in pat]
+ # base case
+ dp[0][ord(pat[0])] = 1
+ # 影子状态 X 初始化为 0
+ X = 0
+ for j in range(1, M):
+ for c in range(256):
+ dp[j][c] = dp[X][c]
+ dp[j][ord(pat[j])] = j + 1
+ # 更新影子状态
+ X = dp[X][ord(pat[j])]
+
+ N = len(txt)
+ # pat 初始状态为 0
+ j = 0
+ for i in range(N):
+ # 计算 pat 的下一个状态
+ j = dp[j][ord(txt[i])]
+ # 到达终止态,返回结果
+ if j == M:
+ return i - M + 1
+ # 没到达终止态,匹配失败
+ return -1
+```
diff --git a/动态规划系列/动态规划之博弈问题.md b/动态规划系列/动态规划之博弈问题.md
index f9082d8..c75749a 100644
--- a/动态规划系列/动态规划之博弈问题.md
+++ b/动态规划系列/动态规划之博弈问题.md
@@ -11,8 +11,8 @@

相关推荐:
- * [40张图解:TCP三次握手和四次挥手面试题](https://labuladong.gitbook.io/algo)
- * [如何计算完全二叉树的节点数](https://labuladong.gitbook.io/algo)
+ * [40张图解:TCP三次握手和四次挥手面试题](https://labuladong.gitbook.io/algo/)
+ * [如何计算完全二叉树的节点数](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -20,7 +20,7 @@
**-----------**
-上一篇文章 [几道智力题](https://labuladong.gitbook.io/algo) 中讨论到一个有趣的「石头游戏」,通过题目的限制条件,这个游戏是先手必胜的。但是智力题终究是智力题,真正的算法问题肯定不会是投机取巧能搞定的。所以,本文就借石头游戏来讲讲「假设两个人都足够聪明,最后谁会获胜」这一类问题该如何用动态规划算法解决。
+上一篇文章 [几道智力题](https://labuladong.gitbook.io/algo/) 中讨论到一个有趣的「石头游戏」,通过题目的限制条件,这个游戏是先手必胜的。但是智力题终究是智力题,真正的算法问题肯定不会是投机取巧能搞定的。所以,本文就借石头游戏来讲讲「假设两个人都足够聪明,最后谁会获胜」这一类问题该如何用动态规划算法解决。
博弈类问题的套路都差不多,下文参考 [这个 YouTube 视频](https://www.youtube.com/watch?v=WxpIHvsu1RI) 的思路讲解,其核心思路是在二维 dp 的基础上使用元组分别存储两个人的博弈结果。掌握了这个技巧以后,别人再问你什么俩海盗分宝石,俩人拿硬币的问题,你就告诉别人:我懒得想,直接给你写个算法算一下得了。
@@ -207,7 +207,7 @@ int stoneGame(int[] piles) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/动态规划系列/动态规划之四键键盘.md b/动态规划系列/动态规划之四键键盘.md
index e2641f5..312cad1 100644
--- a/动态规划系列/动态规划之四键键盘.md
+++ b/动态规划系列/动态规划之四键键盘.md
@@ -11,8 +11,8 @@

相关推荐:
- * [如何高效寻找素数](https://labuladong.gitbook.io/algo)
- * [动态规划解题套路框架](https://labuladong.gitbook.io/algo)
+ * [如何高效寻找素数](https://labuladong.gitbook.io/algo/)
+ * [动态规划解题套路框架](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -192,7 +192,7 @@ def dp(n, a_num, copy):
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/动态规划系列/动态规划之正则表达.md b/动态规划系列/动态规划之正则表达.md
index 9190afc..a3d13bd 100644
--- a/动态规划系列/动态规划之正则表达.md
+++ b/动态规划系列/动态规划之正则表达.md
@@ -10,8 +10,8 @@

相关推荐:
- * [我写了首诗,把滑动窗口算法算法变成了默写题](https://labuladong.gitbook.io/algo)
- * [二分查找高效判定子序列](https://labuladong.gitbook.io/algo)
+ * [我写了首诗,把滑动窗口算法算法变成了默写题](https://labuladong.gitbook.io/algo/)
+ * [二分查找高效判定子序列](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -287,7 +287,7 @@ bool dp(string& s, int i, string& p, int j) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/动态规划系列/动态规划设计:最长递增子序列.md b/动态规划系列/动态规划设计:最长递增子序列.md
index edef473..55aeb8d 100644
--- a/动态规划系列/动态规划设计:最长递增子序列.md
+++ b/动态规划系列/动态规划设计:最长递增子序列.md
@@ -10,8 +10,8 @@

相关推荐:
- * [动态规划设计:最大子数组](https://labuladong.gitbook.io/algo)
- * [一文学会递归解题](https://labuladong.gitbook.io/algo)
+ * [动态规划设计:最大子数组](https://labuladong.gitbook.io/algo/)
+ * [一文学会递归解题](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -19,7 +19,7 @@
**-----------**
-也许有读者看了前文 [动态规划详解](https://labuladong.gitbook.io/algo),学会了动态规划的套路:找到了问题的「状态」,明确了 `dp` 数组/函数的含义,定义了 base case;但是不知道如何确定「选择」,也就是不到状态转移的关系,依然写不出动态规划解法,怎么办?
+也许有读者看了前文 [动态规划详解](https://labuladong.gitbook.io/algo/),学会了动态规划的套路:找到了问题的「状态」,明确了 `dp` 数组/函数的含义,定义了 base case;但是不知道如何确定「选择」,也就是不到状态转移的关系,依然写不出动态规划解法,怎么办?
不要担心,动态规划的难点本来就在于寻找正确的状态转移方程,本文就借助经典的「最长递增子序列问题」来讲一讲设计动态规划的通用技巧:**数学归纳思想**。
@@ -43,7 +43,7 @@
**我们的定义是这样的:`dp[i]` 表示以 `nums[i]` 这个数结尾的最长递增子序列的长度。**
-PS:为什么这样定义呢?这是解决子序列问题的一个套路,后文[动态规划之子序列问题解题模板](https://labuladong.gitbook.io/algo) 总结了几种常见套路。你读完本章所有的动态规划问题,就会发现 `dp` 数组的定义方法也就那几种。
+PS:为什么这样定义呢?这是解决子序列问题的一个套路,后文[动态规划之子序列问题解题模板](https://labuladong.gitbook.io/algo/) 总结了几种常见套路。你读完本章所有的动态规划问题,就会发现 `dp` 数组的定义方法也就那几种。
根据这个定义,我们就可以推出 base case:`dp[i]` 初始值为 1,因为以 `nums[i]` 结尾的最长递增子序列起码要包含它自己。
@@ -164,7 +164,7 @@ public int lengthOfLIS(int[] nums) {
我们只要把处理扑克牌的过程编程写出来即可。每次处理一张扑克牌不是要找一个合适的牌堆顶来放吗,牌堆顶的牌不是**有序**吗,这就能用到二分查找了:用二分查找来搜索当前牌应放置的位置。
-PS:旧文[二分查找算法详解](https://labuladong.gitbook.io/algo)详细介绍了二分查找的细节及变体,这里就完美应用上了,如果没读过强烈建议阅读。
+PS:旧文[二分查找算法详解](https://labuladong.gitbook.io/algo/)详细介绍了二分查找的细节及变体,这里就完美应用上了,如果没读过强烈建议阅读。
```java
public int lengthOfLIS(int[] nums) {
@@ -207,7 +207,7 @@ public int lengthOfLIS(int[] nums) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/动态规划系列/动态规划详解进阶.md b/动态规划系列/动态规划详解进阶.md
index f51608e..6ce71fa 100644
--- a/动态规划系列/动态规划详解进阶.md
+++ b/动态规划系列/动态规划详解进阶.md
@@ -11,8 +11,8 @@

相关推荐:
- * [经典动态规划:完全背包问题](https://labuladong.gitbook.io/algo)
- * [Git/SQL/正则表达式的在线练习平台](https://labuladong.gitbook.io/algo)
+ * [经典动态规划:完全背包问题](https://labuladong.gitbook.io/algo/)
+ * [Git/SQL/正则表达式的在线练习平台](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -216,7 +216,7 @@ int coinChange(int[] coins, int amount);
回到凑零钱问题,为什么说它符合最优子结构呢?比如你想求 `amount = 11` 时的最少硬币数(原问题),如果你知道凑出 `amount = 10` 的最少硬币数(子问题),你只需要把子问题的答案加一(再选一枚面值为 1 的硬币)就是原问题的答案。因为硬币的数量是没有限制的,所以子问题之间没有相互制,是互相独立的。
-PS:关于最优子结构的问题,后文[动态规划答疑篇](https://labuladong.gitbook.io/algo) 还会再举例探讨。
+PS:关于最优子结构的问题,后文[动态规划答疑篇](https://labuladong.gitbook.io/algo/) 还会再举例探讨。
那么,既然知道了这是个动态规划问题,就要思考**如何列出正确的状态转移方程**?
@@ -360,7 +360,7 @@ PS:为啥 `dp` 数组初始化为 `amount + 1` 呢,因为凑成 `amount` 金
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/动态规划系列/团灭股票问题.md b/动态规划系列/团灭股票问题.md
index 61f64a3..f34d173 100644
--- a/动态规划系列/团灭股票问题.md
+++ b/动态规划系列/团灭股票问题.md
@@ -11,8 +11,8 @@

相关推荐:
- * [动态规划之KMP字符匹配算法](https://labuladong.gitbook.io/algo)
- * [如何判断回文链表](https://labuladong.gitbook.io/algo)
+ * [动态规划之KMP字符匹配算法](https://labuladong.gitbook.io/algo/)
+ * [如何判断回文链表](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -420,7 +420,7 @@ int maxProfit_k_any(int max_k, int[] prices) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/动态规划系列/子序列问题模板.md b/动态规划系列/子序列问题模板.md
index c7a3d5a..1f2570a 100644
--- a/动态规划系列/子序列问题模板.md
+++ b/动态规划系列/子序列问题模板.md
@@ -12,8 +12,8 @@

相关推荐:
- * [洗牌算法](https://labuladong.gitbook.io/algo)
- * [twoSum问题的核心思想](https://labuladong.gitbook.io/algo)
+ * [洗牌算法](https://labuladong.gitbook.io/algo/)
+ * [twoSum问题的核心思想](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -166,7 +166,7 @@ int longestPalindromeSubseq(string s) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,labuladong 带你搞定 LeetCode**。
diff --git a/动态规划系列/抢房子.md b/动态规划系列/抢房子.md
index 081c583..f2e2541 100644
--- a/动态规划系列/抢房子.md
+++ b/动态规划系列/抢房子.md
@@ -10,7 +10,7 @@

相关推荐:
- * [动态规划之四键键盘](https://labuladong.gitbook.io/algo)
+ * [动态规划之四键键盘](https://labuladong.gitbook.io/algo/)
* [经典动态规划:子集背包问题](/https://labuladong.gitbook.io/algo)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -250,7 +250,7 @@ int[] dp(TreeNode root) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/动态规划系列/最优子结构.md b/动态规划系列/最优子结构.md
index caf68cf..808f6a6 100644
--- a/动态规划系列/最优子结构.md
+++ b/动态规划系列/最优子结构.md
@@ -12,8 +12,8 @@

相关推荐:
- * [搜索引擎背后的经典数据结构和算法](https://labuladong.gitbook.io/algo)
- * [动态规划之四键键盘](https://labuladong.gitbook.io/algo)
+ * [搜索引擎背后的经典数据结构和算法](https://labuladong.gitbook.io/algo/)
+ * [动态规划之四键键盘](https://labuladong.gitbook.io/algo/)
**-----------**
@@ -54,7 +54,7 @@ return result;
当然,上面这个例子太简单了,不过请读者回顾一下,我们做动态规划问题,是不是一直在求各种最值,本质跟我们举的例子没啥区别,无非需要处理一下重叠子问题。
-前文不[同定义不同解法](https://labuladong.gitbook.io/algo) 和 [高楼扔鸡蛋进阶](https://labuladong.gitbook.io/algo) 就展示了如何改造问题,不同的最优子结构,可能导致不同的解法和效率。
+前文不[同定义不同解法](https://labuladong.gitbook.io/algo/) 和 [高楼扔鸡蛋进阶](https://labuladong.gitbook.io/algo/) 就展示了如何改造问题,不同的最优子结构,可能导致不同的解法和效率。
再举个常见但也十分简单的例子,求一棵二叉树的最大值,不难吧(简单起见,假设节点中的值都是非负数):
@@ -149,7 +149,7 @@ for (int i = 1; i < m; i++)
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/动态规划系列/最长公共子序列.md b/动态规划系列/最长公共子序列.md
index bff1721..1fe1cfb 100644
--- a/动态规划系列/最长公共子序列.md
+++ b/动态规划系列/最长公共子序列.md
@@ -11,8 +11,8 @@

相关推荐:
- * [回溯算法解题套路框架](https://labuladong.gitbook.io/algo)
- * [经典动态规划:高楼扔鸡蛋(进阶)](https://labuladong.gitbook.io/algo)
+ * [回溯算法解题套路框架](https://labuladong.gitbook.io/algo/)
+ * [经典动态规划:高楼扔鸡蛋(进阶)](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -139,7 +139,7 @@ else:
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
@@ -149,6 +149,7 @@ else:
======其他语言代码======
+### c++
[Edwenc](https://github.com/Edwenc) 提供 C++ 代码:
@@ -183,6 +184,8 @@ public:
};
```
+### java
+
[Shawn](https://github.com/Shawn-Hx) 提供 Java 代码:
```java
@@ -207,4 +210,28 @@ public int longestCommonSubsequence(String text1, String text2) {
}
```
+### python
+
+[lo-tp](http://blog.lotp.xyz/) 提供 Python 代码:
+
+```python
+class Solution(object):
+ def longestCommonSubsequence(self, text1, text2):
+ # calculate the size of the first and second string
+ sz1, sz2 = len(text1), len(text2)
+ # since to calculate dp(i,j) we only need dp(i-1,j-1), dp(i-1,j), dp(i,j-1)
+ # we don't have to save data before i-1
+ # we use dp to save dp(i-1, 0), dp(i-1, 1)....dp(i-1, sz2)
+ # we use tmp to save dp(i, 0), dp(i,1)....(dpi-1, sz2)
+ tmp, dp = [0]*(sz2+1), [0]*(sz2+1)
+ for i in range(0, sz1):
+ for j in range(0, sz2):
+ tmp[j+1] = dp[j] + \
+ 1 if text1[i] == text2[j] else max(tmp[j], dp[j+1])
+ # In the next iteration, we will calculate dp(i+1,0),dp(i+1, 1)....dp(i+1,sz2)
+ # So we exchange dp and tmp
+ tmp, dp = dp, tmp
+ return dp[-1]
+```
+
diff --git a/动态规划系列/编辑距离.md b/动态规划系列/编辑距离.md
index 8ba18e4..5d634fb 100644
--- a/动态规划系列/编辑距离.md
+++ b/动态规划系列/编辑距离.md
@@ -11,8 +11,8 @@

相关推荐:
- * [labuladong优质作者扶持计划](https://labuladong.gitbook.io/algo)
- * [动态规划设计:最大子数组](https://labuladong.gitbook.io/algo)
+ * [labuladong优质作者扶持计划](https://labuladong.gitbook.io/algo/)
+ * [动态规划设计:最大子数组](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -283,7 +283,7 @@ class Node {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
@@ -291,4 +291,31 @@ class Node {
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+[ChenjieXu](https://github.com/ChenjieXu) 提供Python版本代码:
+
+```python3
+def minDistance(word1, word2):
+ m, n = len(word1), len(word2)
+ # 创建 DP 数组
+ dp = [[0] * (n + 1) for _ in range(m + 1)]
+
+ # base case初始化
+ for i in range(m + 1):
+ dp[i][0] = i
+ for j in range(n + 1):
+ dp[0][j] = j
+
+ # 自底向上求解
+ for i in range(1, m + 1):
+ for j in range(1, n + 1):
+ # 状态转移方程
+ if word1[i - 1] == word2[j - 1]:
+ dp[i][j] = dp[i - 1][j - 1]
+ else:
+ dp[i][j] = min(dp[i - 1][j] + 1,
+ dp[i][j - 1] + 1,
+ dp[i - 1][j - 1] + 1)
+ # 储存着整个 word1 和 word2 的最小编辑距离
+ return dp[m][n]
+````
\ No newline at end of file
diff --git a/动态规划系列/贪心算法之区间调度问题.md b/动态规划系列/贪心算法之区间调度问题.md
index 70e27fc..268654b 100644
--- a/动态规划系列/贪心算法之区间调度问题.md
+++ b/动态规划系列/贪心算法之区间调度问题.md
@@ -1,4 +1,4 @@
-# 贪心算法之区间调度问题
+# 贪心算法之区间调度问题
@@ -11,8 +11,8 @@

相关推荐:
- * [如何判定括号合法性](https://labuladong.gitbook.io/algo)
- * [一文解决三道区间问题](https://labuladong.gitbook.io/algo)
+ * [如何判定括号合法性](https://labuladong.gitbook.io/algo/)
+ * [一文解决三道区间问题](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -150,7 +150,7 @@ int findMinArrowShots(int[][] intvs) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
@@ -158,4 +158,43 @@ int findMinArrowShots(int[][] intvs) {
@@ -11,8 +11,8 @@

相关推荐:
- * [特殊数据结构:单调队列](https://labuladong.gitbook.io/algo)
- * [一行代码就能解决的算法题](https://labuladong.gitbook.io/algo)
+ * [特殊数据结构:单调队列](https://labuladong.gitbook.io/algo/)
+ * [一行代码就能解决的算法题](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -28,7 +28,7 @@
**-----------**
-通过之前的文章[框架思维](https://labuladong.gitbook.io/algo),二叉树的遍历框架应该已经印到你的脑子里了,这篇文章就来实操一下,看看框架思维是怎么灵活运用,秒杀一切二叉树问题的。
+通过之前的文章[框架思维](https://labuladong.gitbook.io/algo/),二叉树的遍历框架应该已经印到你的脑子里了,这篇文章就来实操一下,看看框架思维是怎么灵活运用,秒杀一切二叉树问题的。
二叉树算法的设计的总路线:明确一个节点要做的事情,然后剩下的事抛给框架。
@@ -302,7 +302,7 @@ void BST(TreeNode root, int target) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
@@ -310,9 +310,14 @@ void BST(TreeNode root, int target) {
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+### java
+
+[ZakAnun](https://github.com/ZakAnun) 提供代码
+
+```java
+// 496.下一个更大元素
+// 暴力解法
+public int[] nextGreaterElement(int[] nums1, int[] nums2) {
+ int[] result = new int[nums1.length];
+ for (int i = 0; i < nums1.length; i++) {
+ // 需要记录第一个数组每个元素在第二个数组中出现的位置
+ int index = 0;
+ for (int j = 0; j < nums2.length; j++) {
+ if (nums1[i] == nums2[j]) {
+ index = j;
+ break;
+ }
+ }
+ // 根据找到的位置往后遍历,若符合条件则记录到结果数组
+ for (int k = index; k < nums2.length; k++) {
+ if (nums2[k] > nums1[i]) {
+ result[i] = nums2[k];
+ break;
+ }
+ }
+ // 判断若对应位置结果依然为默认值,则将其修改为 -1
+ if (result[i] == 0) {
+ result[i] = -1;
+ }
+ }
+ return result;
+}
+
+// 分析: 暴力解法中需要确定数组1中每个元素在数组2中的下标而需要进行额外的遍历导致时间复杂度升高,
+// 但若能够先罗列出全部的结果,然后从结果集中获取数组1中每个元素对应的下一个更大元素,就可以节省这部分时间(这里需要引用 HashMap 帮助我们记录结果,以便根据数组1获取。
+// 单调栈解法
+public int[] nextGreaterElement(int[] nums1, int[] nums2) {
+ Stack
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+### python3
+
+由[SCUHZS](ttps://github.com/brucecat)提供
+
+
+```python
+from collections import deque
+
+class MonotonicQueue(object):
+ def __init__(self):
+ # 双端队列
+ self.data = deque()
+
+ def push(self, n):
+ # 实现单调队列的push方法
+ while self.data and self.data[-1] < n:
+ self.data.pop()
+ self.data.append(n)
+
+ def max(self):
+ # 取得单调队列中的最大值
+ return self.data[0]
+
+ def pop(self, n):
+ # 实现单调队列的pop方法
+ if self.data and self.data[0] == n:
+ self.data.popleft()
+
+
+class Solution:
+ def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
+ # 单调队列实现的窗口
+ window = MonotonicQueue()
+
+ # 结果
+ res = []
+
+ for i in range(0, len(nums)):
+
+ if i < k-1:
+ # 先填满窗口前k-1
+ window.push(nums[i])
+ else:
+ # 窗口向前滑动
+ window.push(nums[i])
+ res.append(window.max())
+ window.pop(nums[i-k+1])
+ return res
+
+```
+
+### java
+
+```java
+class Solution {
+ public int[] maxSlidingWindow(int[] nums, int k) {
+ int len = nums.length;
+ // 判断数组或者窗口长度为0的情况
+ if (len * k == 0) {
+ return new int[0];
+ }
+
+ /*
+ 采用两端扫描的方法
+ 将数组分成大小为 k 的若干个窗口, 对每个窗口分别从左往右和从右往左扫描, 记录扫描的最大值
+ left[] 记录从左往右扫描的最大值
+ right[] 记录从右往左扫描的最大值
+ */
+ int[] left = new int[len];
+ int[] right = new int[len];
+
+ for (int i = 0; i < len; i = i + k) {
+ // 每个窗口中的第一个值
+ left[i] = nums[i];
+ // 窗口的最后边界
+ int index = i + k - 1 >= len ? len - 1 : i + k - 1;
+ // 每个窗口的最后一个值
+ right[index] = nums[index];
+ // 对该窗口从左往右扫描
+ for (int j = i + 1; j <= index; j++) {
+ left[j] = Math.max(left[j - 1], nums[j]);
+ }
+ // 对该窗口从右往左扫描
+ for (int j = index - 1; j >= i; j--) {
+ right[j] = Math.max(right[j + 1], nums[j]);
+ }
+ }
+
+ int[] arr = new int[len - k + 1];
+
+ // 对于第 i 个位置, 它一定是该窗口从右往左扫描数组中的最后一个值, 相对的 i + k - 1 是该窗口从左向右扫描数组中的最后一个位置
+ // 对两者取最大值即可
+ for (int i = 0; i < len - k + 1; i++) {
+ arr[i] = Math.max(right[i], left[i + k - 1]);
+ }
+
+ return arr;
+ }
+}
+```
diff --git a/数据结构系列/实现计算器.md b/数据结构系列/实现计算器.md
index 2b980f7..384a86f 100644
--- a/数据结构系列/实现计算器.md
+++ b/数据结构系列/实现计算器.md
@@ -12,8 +12,8 @@

相关推荐:
- * [特殊数据结构:单调队列](https://labuladong.gitbook.io/algo)
- * [一行代码就能解决的算法题](https://labuladong.gitbook.io/algo)
+ * [特殊数据结构:单调队列](https://labuladong.gitbook.io/algo/)
+ * [一行代码就能解决的算法题](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -298,7 +298,7 @@ def calculate(s: str) -> int:
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/数据结构系列/设计Twitter.md b/数据结构系列/设计Twitter.md
index 399fbbb..96db455 100644
--- a/数据结构系列/设计Twitter.md
+++ b/数据结构系列/设计Twitter.md
@@ -11,8 +11,8 @@

相关推荐:
- * [面试官:你说对MySQL事务很熟?那我问你10个问题](https://labuladong.gitbook.io/algo)
- * [一行代码就能解决的算法题](https://labuladong.gitbook.io/algo)
+ * [面试官:你说对MySQL事务很熟?那我问你10个问题](https://labuladong.gitbook.io/algo/)
+ * [一行代码就能解决的算法题](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -294,7 +294,7 @@ PS:本文前两张图片和 GIF 是我第一次尝试用平板的绘图软件
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
@@ -302,4 +302,121 @@ PS:本文前两张图片和 GIF 是我第一次尝试用平板的绘图软件
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+[happy-yuxuan](https://github.com/happy-yuxuan) 提供 C++ 代码:
+
+```c++
+static int timestamp = 0;
+class Tweet {
+private:
+ int id;
+ int time;
+public:
+ Tweet *next;
+ // id为推文内容,time为发文时间
+ Tweet(int id, int time) {
+ this->id = id;
+ this->time = time;
+ next = nullptr;
+ }
+ int getId() const {
+ return this->id;
+ }
+ int getTime() const {
+ return this->time;
+ }
+};
+class User {
+private:
+ int id;
+public:
+ Tweet *head; // 发布的Twitter,用链表表示
+ unordered_set
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+### c++
+
+[shilei](https://github.com/ShileiGuo) 提供C++解法代码:
+
+思想:
+
+ 1.head表示需要反转的头节点,pre表示需要反转头节点的前驱节点
+
+ 2.对于从m到n的节点反转,需要反转n-m次,将head的next节点移动到需要反转链表部分的首部,需要反转链表部分剩余节点依旧保持相对顺序即可
+
+ 3.示例 当m=2, n=5时
+
+ 第一次反转:1(pre) 2(head) 3(next) 4 5 反转为 1 3 2 4 5
+
+ 第二次反转:1(pre) 3 2(head) 4(next) 5 反转为 1 4 3 2 5
+
+ 第三次发转:1(pre) 4 3 2(head) 5(next) 反转为 1 5 4 3 2
+
+```CPP
+class Solution {
+public:
+ ListNode* reverseBetween(ListNode* head, int m, int n) {
+ //初始化哨兵节点
+ ListNode* dummy=new ListNode(-1);
+ //初始化待反转区间的前一个节点
+ ListNode* pre=dummy;
+ //哨兵节点下一个节点指向head头节点
+ dummy->next=head;
+
+ //获取待反转节点的前一个节点
+ for(int i=0;i
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+第261题的Java代码(提供:[LEODPEN](https://github.com/LEODPEN))
+
+```java
+class Solution {
+
+ class DisjointSet {
+
+ int count; // 连通分量的总个数
+ int[] parent; // 每个节点的头节点(不一定是连通分量的最终头节点)
+ int[] size; // 每个连通分量的大小
+
+ public DisjointSet(int n) {
+ parent = new int[n];
+ size = new int[n];
+ // 初为n个连通分量,期望最后为1
+ count = n;
+ for (int i = 0; i < n; i++) {
+ // 初始的连通分量只有该节点本身
+ parent[i] = i;
+ size[i] = 1;
+ }
+ }
+
+ /**
+ * @param first 节点1
+ * @param second 节点2
+ * @return 未连通 && 连通成功
+ */
+ public boolean union(int first, int second) {
+ // 分别找到包含first 和 second 的最终根节点
+ int firstParent = findRootParent(first), secondParent = findRootParent(second);
+ // 相等说明已经处于一个连通分量,即说明有环
+ if (firstParent == secondParent) return false;
+ // 将较小的连通分量融入较大的连通分量
+ if (size[firstParent] >= size[secondParent]) {
+ parent[secondParent] = firstParent;
+ size[firstParent] += size[secondParent];
+ } else {
+ parent[firstParent] = secondParent;
+ size[secondParent] += size[firstParent];
+ }
+ // 连通分量已合并,count减少
+ count--;
+ return true;
+ }
+
+ /**
+ * @param node 某节点
+ * @return 包含该节点的连通分量的最终根节点
+ */
+ private int findRootParent(int node) {
+ while (node != parent[node]) {
+ // 压缩路径
+ parent[node] = parent[parent[node]];
+ node = parent[node];
+ }
+ return node;
+ }
+ }
+
+ public boolean validTree(int n, int[][] edges) {
+ // 树的特性:节点数 = 边数 + 1
+ if (edges.length != n - 1) return false;
+ DisjointSet djs = new DisjointSet(n);
+ for (int[] edg : edges) {
+ // 判断连通情况(如果合并的两个点在一个连通分量里,说明有环)
+ if (!djs.union(edg[0], edg[1])) return false;
+ }
+ // 是否全部节点均已相连
+ return djs.count == 1;
+ }
+}
+```
diff --git a/算法思维系列/UnionFind算法详解.md b/算法思维系列/UnionFind算法详解.md
index 977d8ba..32e37de 100644
--- a/算法思维系列/UnionFind算法详解.md
+++ b/算法思维系列/UnionFind算法详解.md
@@ -11,8 +11,8 @@

相关推荐:
- * [一文秒杀四道原地修改数组的算法题](https://labuladong.gitbook.io/algo)
- * [学习算法和数据结构的思路指南](https://labuladong.gitbook.io/algo)
+ * [一文秒杀四道原地修改数组的算法题](https://labuladong.gitbook.io/algo/)
+ * [学习算法和数据结构的思路指南](https://labuladong.gitbook.io/algo/)
**-----------**
@@ -309,7 +309,7 @@ Union-Find 算法的复杂度可以这样分析:构造函数初始化数据结
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/算法思维系列/twoSum问题的核心思想.md b/算法思维系列/twoSum问题的核心思想.md
index 327fc23..a1a92c5 100644
--- a/算法思维系列/twoSum问题的核心思想.md
+++ b/算法思维系列/twoSum问题的核心思想.md
@@ -11,8 +11,8 @@

相关推荐:
- * [我写了首诗,让你闭着眼睛也能写对二分搜索](https://labuladong.gitbook.io/algo)
- * [经典动态规划:完全背包问题](https://labuladong.gitbook.io/algo)
+ * [我写了首诗,让你闭着眼睛也能写对二分搜索](https://labuladong.gitbook.io/algo/)
+ * [经典动态规划:完全背包问题](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -177,7 +177,7 @@ int[] twoSum(int[] nums, int target) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
@@ -185,4 +185,29 @@ int[] twoSum(int[] nums, int target) {
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+由[JodyZ0203](https://github.com/JodyZ0203)提供 1. Two Sums Python3 解法代码:
+
+;; 只用一个哈希表
+
+```Python
+class Solution:
+ def twoSum(self, nums, target):
+ """
+ :type nums: List[int]
+ :type target: int
+ :rtype: List[int]
+ """
+ # 提前构造一个哈希表
+ hashTable = {}
+ # 寻找两个目标数值
+ for i, n in enumerate(nums):
+ other_num = target - n
+ # 如果存在这个余数 other_num
+ if other_num in hashTable.keys():
+ # 查看是否存在哈希表里,如果存在的话就返回数组
+ return [i, hashTable[other_num]]
+ # 如果不存在的话继续处理剩余的数
+ hashTable[n] = i
+```
diff --git a/算法思维系列/为什么推荐算法4.md b/算法思维系列/为什么推荐算法4.md
index 8792b9c..ff24326 100644
--- a/算法思维系列/为什么推荐算法4.md
+++ b/算法思维系列/为什么推荐算法4.md
@@ -11,8 +11,8 @@

相关推荐:
- * [递归反转链表的一部分](https://labuladong.gitbook.io/algo)
- * [25 张图解:键入网址后,到网页显示,其间发生了什么](https://labuladong.gitbook.io/algo)
+ * [递归反转链表的一部分](https://labuladong.gitbook.io/algo/)
+ * [25 张图解:键入网址后,到网页显示,其间发生了什么](https://labuladong.gitbook.io/algo/)
**-----------**
@@ -93,7 +93,7 @@
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/算法思维系列/二分查找详解.md b/算法思维系列/二分查找详解.md
index a413541..49f8f7f 100644
--- a/算法思维系列/二分查找详解.md
+++ b/算法思维系列/二分查找详解.md
@@ -11,8 +11,8 @@

相关推荐:
- * [手把手带你刷二叉树(第三期)](https://labuladong.gitbook.io/algo)
- * [回溯算法解题套路框架](https://labuladong.gitbook.io/algo)
+ * [手把手带你刷二叉树(第三期)](https://labuladong.gitbook.io/algo/)
+ * [回溯算法解题套路框架](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -500,7 +500,7 @@ int right_bound(int[] nums, int target) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
@@ -508,4 +508,98 @@ int right_bound(int[] nums, int target) {
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+[MarineJoker](https://github.com/MarineJoker) 提供 Python3 代码
+
+```python
+# 基本二分搜索
+def binarySearch(nums, target):
+ left = 0
+ right = len(nums) - 1
+ while left <= right:
+ mid = (left + right) // 2
+ if nums[mid] == target:
+ # 直接返回
+ return mid
+ elif nums[mid] < target:
+ left = mid + 1
+ elif nums[mid] > target:
+ right = mid - 1
+ # 直接返回
+ return -1
+
+
+# 寻找左侧边界的二分搜索,开区间写法
+def left_bound(nums, target):
+ left, right = 0, len(nums)
+ if right == 0:
+ return -1
+ while left < right:
+ mid = (left + right) // 2
+ if nums[mid] == target:
+ # 锁定左侧边界
+ right = mid
+ elif nums[mid] < target:
+ left = mid + 1
+ elif nums[mid] > target:
+ right = mid
+ # 检查left越界情况
+ if left >= len(nums) or nums[left] != target:
+ return -1
+ return left
+
+
+# 寻找右侧边界的二分搜索,开区间写法
+def right_bound(nums, target):
+ left, right = 0, len(nums)
+ if right == 0:
+ return -1
+ while left < right:
+ mid = (left + right) // 2
+ if nums[mid] == target:
+ # 锁定右侧边界
+ left = mid + 1
+ elif nums[mid] < target:
+ left = mid + 1
+ elif nums[mid] > target:
+ right = mid
+ # 检查越界情况
+ if left == 0 or nums[left - 1] != target:
+ return -1
+ return left - 1
+
+
+# 寻找左侧边界的二分搜索,闭区间写法
+def left_bound(nums, target):
+ left, right = 0, len(nums) - 1
+ while left <= right:
+ mid = (left + right) // 2
+ if nums[mid] == target:
+ # 锁定左侧边界
+ right = mid - 1
+ elif nums[mid] < target:
+ left = mid + 1
+ elif nums[mid] > target:
+ right = mid - 1
+ # 检查left越界情况
+ if left >= len(nums) or nums[left] != target:
+ return -1
+ return left
+
+# 寻找右侧边界的二分搜索,闭区间写法
+def right_bound(nums, target):
+ left, right = 0, len(nums) - 1
+ while left <= right:
+ mid = (left + right) // 2
+ if nums[mid] == target:
+ # 锁定右侧边界
+ left = mid + 1
+ elif nums[mid] < target:
+ left = mid + 1
+ elif nums[mid] > target:
+ right = mid - 1
+ # 检查right越界情况
+ if right < 0 or nums[right] != target:
+ return -1
+ return right
+```
diff --git a/算法思维系列/信封嵌套问题.md b/算法思维系列/信封嵌套问题.md
index bd6997f..a31453c 100644
--- a/算法思维系列/信封嵌套问题.md
+++ b/算法思维系列/信封嵌套问题.md
@@ -11,8 +11,8 @@

相关推荐:
- * [讲两道常考的阶乘算法题](https://labuladong.gitbook.io/algo)
- * [状态压缩:对动态规划进行降维打击](https://labuladong.gitbook.io/algo)
+ * [讲两道常考的阶乘算法题](https://labuladong.gitbook.io/algo/)
+ * [状态压缩:对动态规划进行降维打击](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -22,7 +22,7 @@
很多算法问题都需要排序技巧,其难点不在于排序本身,而是需要巧妙地排序进行预处理,将算法问题进行转换,为之后的操作打下基础。
-信封嵌套问题就需要先按特定的规则排序,之后就转换为一个 [最长递增子序列问题](https://labuladong.gitbook.io/algo),可以用前文 [二分查找详解](https://labuladong.gitbook.io/algo) 的技巧来解决了。
+信封嵌套问题就需要先按特定的规则排序,之后就转换为一个 [最长递增子序列问题](https://labuladong.gitbook.io/algo/),可以用前文 [二分查找详解](https://labuladong.gitbook.io/algo/) 的技巧来解决了。
### 一、题目概述
@@ -127,7 +127,7 @@ public int lengthOfLIS(int[] nums) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/算法思维系列/几个反直觉的概率问题.md b/算法思维系列/几个反直觉的概率问题.md
index 767a11f..5a3adae 100644
--- a/算法思维系列/几个反直觉的概率问题.md
+++ b/算法思维系列/几个反直觉的概率问题.md
@@ -11,12 +11,12 @@

相关推荐:
- * [学习算法和数据结构的思路指南](https://labuladong.gitbook.io/algo)
- * [我写了首诗,让你闭着眼睛也能写对二分搜索](https://labuladong.gitbook.io/algo)
+ * [学习算法和数据结构的思路指南](https://labuladong.gitbook.io/algo/)
+ * [我写了首诗,让你闭着眼睛也能写对二分搜索](https://labuladong.gitbook.io/algo/)
**-----------**
-上篇文章 [洗牌算法详解](https://labuladong.gitbook.io/algo) 讲到了验证概率算法的蒙特卡罗方法,今天聊点轻松的内容:几个和概率相关的有趣问题。
+上篇文章 [洗牌算法详解](https://labuladong.gitbook.io/algo/) 讲到了验证概率算法的蒙特卡罗方法,今天聊点轻松的内容:几个和概率相关的有趣问题。
计算概率有下面两个最简单的原则:
@@ -133,7 +133,7 @@
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/算法思维系列/前缀和技巧.md b/算法思维系列/前缀和技巧.md
index ef96986..5a3d3ce 100644
--- a/算法思维系列/前缀和技巧.md
+++ b/算法思维系列/前缀和技巧.md
@@ -11,8 +11,8 @@

相关推荐:
- * [如何去除有序数组的重复元素](https://labuladong.gitbook.io/algo)
- * [区间调度之区间合并问题](https://labuladong.gitbook.io/algo)
+ * [如何去除有序数组的重复元素](https://labuladong.gitbook.io/algo/)
+ * [区间调度之区间合并问题](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -151,7 +151,7 @@ for (int i = 1; i < count.length; i++)
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/算法思维系列/区间交集问题.md b/算法思维系列/区间交集问题.md
index b2ca238..b84e324 100644
--- a/算法思维系列/区间交集问题.md
+++ b/算法思维系列/区间交集问题.md
@@ -11,8 +11,8 @@

相关推荐:
- * [经典动态规划:编辑距离](https://labuladong.gitbook.io/algo)
- * [经典动态规划:高楼扔鸡蛋(进阶)](https://labuladong.gitbook.io/algo)
+ * [经典动态规划:编辑距离](https://labuladong.gitbook.io/algo/)
+ * [经典动态规划:高楼扔鸡蛋(进阶)](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -125,7 +125,7 @@ def intervalIntersection(A, B):
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
@@ -133,4 +133,27 @@ def intervalIntersection(A, B):
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+[KiraZh](https://github.com/KiraZh)提供第986题Java代码
+```java
+class Solution {
+ public int[][] intervalIntersection(int[][] A, int[][] B) {
+ List
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+由[kepler-zc](https://github.com/kepler-zc) 提供 51.N皇后 Java 解法代码:
+```java
+class solution {
+ private List
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+### python
+
+[fengshuu](https://github.com/fengshuu) 提供 Python 解法代码:
+```python
+def multiply(num1: str, num2: str) -> str:
+ m, n = len(num1), len(num2)
+ # 结果最多为 m + n 位数
+ res = [0] * (m + n)
+ # 从个位数开始逐位相乘
+ for i in range(m-1, -1, -1):
+ for j in range(n-1, -1, -1):
+ mul = int(num1[i]) * int(num2[j])
+ # 乘积在 res 对应的索引位置
+ p1 = i + j
+ p2 = i + j + 1
+ # 叠加到 res 上
+ digit_sum = mul + res[p2]
+ res[p2] = digit_sum % 10
+ res[p1] += digit_sum // 10
+
+ # 结果前缀可能存的 0(未使用的位)
+ i = 0
+ while i < len(res) and res[i] == 0:
+ i += 1
+
+ # 将计算结果转化成字符串
+ result_str = "".join(str(x) for x in res[i:])
+
+ return "0" if len(result_str) == 0 else result_str
+```
+
+### java
+
+[Zane Wang](https://github.com/zanecat) 提供 Java 解法代码:
+```java
+public String multiply(String num1, String num2) {
+ // 初始化字符数组
+ char[] s1 = num1.toCharArray();
+ char[] s2 = num2.toCharArray();
+
+ // 结果长度最多为两字符串长度之和
+ int[] res = new int[s1.length + s2.length];
+
+ // 从个位开始遍历,把两数字中每一位相乘
+ for (int i = s1.length - 1; i >= 0; i--) {
+ for (int j = s2.length - 1; j >= 0; j--) {
+ // 计算乘积,并把乘积放在 res 对应的位置, 暂时不考虑进位
+ res[i + j + 1] += (s1[i] - '0') * (s2[j] - '0');
+ }
+ }
+
+ // 从个位再次遍历,如果上一次遍历中两数乘积为两位数,进位并叠加到前面一位
+ int carry = 0;
+ for (int i = res.length - 1; i >= 0; i--) {
+ int sum = res[i] + carry;
+ res[i] = sum % 10;
+ carry = sum / 10;
+ }
+
+ //遍历res数组,构造最终答案字符串
+ StringBuilder ans = new StringBuilder();
+ int i = 0;
+
+ // 首先找到不为0的第一位
+ while (i < res.length - 1 && res[i] == 0) {
+ i++;
+ }
+
+ // 将后面的数字附加到ans后面
+ while (i < res.length) {
+ ans.append(res[i++]);
+ }
+ return ans.toString();
+}
+```
\ No newline at end of file
diff --git a/算法思维系列/学习数据结构和算法的高效方法.md b/算法思维系列/学习数据结构和算法的高效方法.md
index a5beaf3..54c0b8d 100644
--- a/算法思维系列/学习数据结构和算法的高效方法.md
+++ b/算法思维系列/学习数据结构和算法的高效方法.md
@@ -11,8 +11,8 @@

相关推荐:
- * [twoSum问题的核心思想](https://labuladong.gitbook.io/algo)
- * [经典动态规划:高楼扔鸡蛋(进阶)](https://labuladong.gitbook.io/algo)
+ * [twoSum问题的核心思想](https://labuladong.gitbook.io/algo/)
+ * [经典动态规划:高楼扔鸡蛋(进阶)](https://labuladong.gitbook.io/algo/)
**-----------**
@@ -130,7 +130,7 @@ N 叉树的遍历又可以扩展为图的遍历,因为图就是好几 N 叉棵
首先要明确的是,**数据结构是工具,算法是通过合适的工具解决特定问题的方法**。也就是说,学习算法之前,最起码得了解那些常用的数据结构,了解它们的特性和缺陷。
-那么该如何在 LeetCode 刷题呢?之前的文章[算法学习之路](https://labuladong.gitbook.io/algo)写过一些,什么按标签刷,坚持下去云云。现在距那篇文章已经过去将近一年了,我不说那些不痛不痒的话,直接说具体的建议:
+那么该如何在 LeetCode 刷题呢?之前的文章[算法学习之路](https://labuladong.gitbook.io/algo/)写过一些,什么按标签刷,坚持下去云云。现在距那篇文章已经过去将近一年了,我不说那些不痛不痒的话,直接说具体的建议:
**先刷二叉树,先刷二叉树,先刷二叉树**!
@@ -214,7 +214,7 @@ void traverse(TreeNode* node) {
再举例吧,说几道我们之前文章写过的问题。
-[动态规划详解](https://labuladong.gitbook.io/algo)说过凑零钱问题,暴力解法就是遍历一棵 N 叉树:
+[动态规划详解](https://labuladong.gitbook.io/algo/)说过凑零钱问题,暴力解法就是遍历一棵 N 叉树:

@@ -247,7 +247,7 @@ def dp(n):
其实很多动态规划问题就是在遍历一棵树,你如果对树的遍历操作烂熟于心,起码知道怎么把思路转化成代码,也知道如何提取别人解法的核心思路。
-再看看回溯算法,前文[回溯算法详解](https://labuladong.gitbook.io/algo)干脆直接说了,回溯算法就是个 N 叉树的前后序遍历问题,没有例外。
+再看看回溯算法,前文[回溯算法详解](https://labuladong.gitbook.io/algo/)干脆直接说了,回溯算法就是个 N 叉树的前后序遍历问题,没有例外。
比如 N 皇后问题吧,主要代码如下:
@@ -286,7 +286,7 @@ N 叉树的遍历框架,找出来了把~你说,树这种结构重不重要
但是,你要是心中没有框架,那么你根本无法解题,给了你答案,你也不会发现这就是个树的遍历问题。
-这种思维是很重要的,[动态规划详解](https://labuladong.gitbook.io/algo)中总结的找状态转移方程的几步流程,有时候按照流程写出解法,说实话我自己都不知道为啥是对的,反正它就是对了。。。
+这种思维是很重要的,[动态规划详解](https://labuladong.gitbook.io/algo/)中总结的找状态转移方程的几步流程,有时候按照流程写出解法,说实话我自己都不知道为啥是对的,反正它就是对了。。。
**这就是框架的力量,能够保证你在快睡着的时候,依然能写出正确的程序;就算你啥都不会,都能比别人高一个级别。**
@@ -301,7 +301,7 @@ N 叉树的遍历框架,找出来了把~你说,树这种结构重不重要
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/算法思维系列/常用的位操作.md b/算法思维系列/常用的位操作.md
index f5b5771..b56e56c 100644
--- a/算法思维系列/常用的位操作.md
+++ b/算法思维系列/常用的位操作.md
@@ -11,8 +11,8 @@

相关推荐:
- * [40张图解:TCP三次握手和四次挥手面试题](https://labuladong.gitbook.io/algo)
- * [动态规划答疑篇](https://labuladong.gitbook.io/algo)
+ * [40张图解:TCP三次握手和四次挥手面试题](https://labuladong.gitbook.io/algo/)
+ * [动态规划答疑篇](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -164,7 +164,7 @@ http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
@@ -172,4 +172,25 @@ http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+由[JodyZ0203](https://github.com/JodyZ0203)提供 191. 位1的个数 Python3 解法代码:
+
+```Python
+class Solution:
+ def hammingWeight(self, n: int) -> int:
+
+ # 先定义一个count,用来存1的出现数量
+ count = 0
+
+ # 只要二进制串不等于0之前,我们用一个循环边消除1和计1的出现数量
+ while n!=0:
+
+ # 用labuladong在文章中所提到的 n&(n-1) 技巧来消除最后一个1
+ n = n & (n-1)
+
+ count+=1
+
+ # 当二进制串全消除完之后,返回1出现的总数量
+ return count
+ ```
diff --git a/算法思维系列/洗牌算法.md b/算法思维系列/洗牌算法.md
index 463a7f1..0ce3b64 100644
--- a/算法思维系列/洗牌算法.md
+++ b/算法思维系列/洗牌算法.md
@@ -11,8 +11,8 @@

相关推荐:
- * [二叉搜索树操作集锦](https://labuladong.gitbook.io/algo)
- * [动态规划解题套路框架](https://labuladong.gitbook.io/algo)
+ * [二叉搜索树操作集锦](https://labuladong.gitbook.io/algo/)
+ * [动态规划解题套路框架](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -203,7 +203,7 @@ for (int feq : count)
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/算法思维系列/滑动窗口技巧.md b/算法思维系列/滑动窗口技巧.md
index 67e6539..c116188 100644
--- a/算法思维系列/滑动窗口技巧.md
+++ b/算法思维系列/滑动窗口技巧.md
@@ -12,8 +12,8 @@
**最新消息:关注公众号参与活动,有机会成为 [70k star 算法仓库](https://github.com/labuladong/fucking-algorithm) 的贡献者,机不可失时不再来**!
相关推荐:
-* [东哥吃葡萄时竟然吃出一道算法题!](https://labuladong.gitbook.io/algo)
-* [如何寻找缺失的元素](https://labuladong.gitbook.io/algo)
+* [东哥吃葡萄时竟然吃出一道算法题!](https://labuladong.gitbook.io/algo/)
+* [如何寻找缺失的元素](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -363,7 +363,7 @@ class Solution:
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
@@ -371,4 +371,31 @@ class Solution:
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+
+
+第3题 Python3 代码(提供: [FaDrYL](https://github.com/FaDrYL) ):
+```Python3
+def lengthOfLongestSubstring(self, s: str) -> int:
+ # 子字符串
+ sub = ""
+ largest = 0
+
+ # 循环字符串,将当前字符加入子字符串,并检查长度
+ for i in range(len(s)):
+ if s[i] not in sub:
+ # 当前字符不存在于子字符串中,加入当前字符
+ sub += s[i]
+ else:
+ # 如果当前子字符串的长度超过了之前的记录
+ if len(sub) > largest:
+ largest = len(sub)
+ # 将子字符串从当前字符处+1切片至最后,并加入当前字符
+ sub = sub[sub.find(s[i])+1:] + s[i]
+
+ # 如果最后的子字符串长度超过了之前的记录
+ if len(sub) > largest:
+ return len(sub)
+ return largest
+```
diff --git a/算法思维系列/烧饼排序.md b/算法思维系列/烧饼排序.md
index 513cbdc..22ed46b 100644
--- a/算法思维系列/烧饼排序.md
+++ b/算法思维系列/烧饼排序.md
@@ -11,8 +11,8 @@

相关推荐:
- * [手把手带你刷二叉树(第三期)](https://labuladong.gitbook.io/algo)
- * [Union-Find算法应用](https://labuladong.gitbook.io/algo)
+ * [手把手带你刷二叉树(第三期)](https://labuladong.gitbook.io/algo/)
+ * [Union-Find算法应用](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -34,7 +34,7 @@

-如何解决这个问题呢?其实类似上篇文章 [递归反转链表的一部分](https://labuladong.gitbook.io/algo),这也是需要**递归思想**的。
+如何解决这个问题呢?其实类似上篇文章 [递归反转链表的一部分](https://labuladong.gitbook.io/algo/),这也是需要**递归思想**的。
### 一、思路分析
@@ -141,7 +141,7 @@ void reverse(int[] arr, int i, int j) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
@@ -149,4 +149,163 @@ void reverse(int[] arr, int i, int j) {
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+### python3
+
+[fengshuu](https://github.com/fengshuu) 提供 Python3 解法代码:
+
+```python
+class Solution:
+ # 记录反转操作序列
+ def __init__(self):
+ self.res = []
+
+ def pancakeSort(self, arr: List[int]) -> List[int]:
+
+ self.sort(arr, len(arr))
+ return self.res
+
+ def sort(self, cakes: List[int], n: int):
+ # base case
+ if 1 == n:
+ return
+
+ # 寻找最大饼的索引
+ max_cake_index = cakes[:n].index(n)
+
+ # 下面进行把最大的饼放到最后的两次翻转
+ # 如果最后一个饼就是最大的, 就不需要翻转, 直接进行下次递归
+ if max_cake_index != n - 1:
+ # 第一次翻转, 将最大饼翻到最上面
+ # 如果第一个饼本来就是最大的, 就不需要第一次翻转.
+ if max_cake_index != 0:
+ cakes[:max_cake_index + 1] = cakes[:max_cake_index + 1][::-1]
+ self.res.append(max_cake_index + 1)
+
+ # 第二次翻转,将最大饼翻到最下面
+ cakes[:n] = cakes[:n][::-1]
+ self.res.append(n)
+
+ # 递归调用
+ self.sort(cakes, n - 1)
+```
+
+
+### c++
+
+[fengshuu](https://github.com/fengshuu) 提供 C++ 解法代码:
+
+```cpp
+class Solution {
+public:
+ vector
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+#### c++
+[cchroot](https://github.com/cchroot) 提供 C++ 代码:
+
+```c++
+class Solution {
+public:
+ int minEatingSpeed(vector
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+由[JodyZ0203](https://github.com/JodyZ0203)提供 292. Nim 游戏 Python3 解法代码:
+
+```Python
+class Solution:
+ def canWinNim(self, n: int) -> bool:
+ # 如果除于是0,说明是4的倍数,所以必输
+ # 否则不是除于不等于0,说明不是4的倍数,说明必胜
+ return n % 4 != 0
+```
+
+由[JodyZ0203](https://github.com/JodyZ0203)提供 877. 石子游戏 Python3 解法代码:
+
+```Python
+class Solution:
+ def stoneGame(self, piles: List[int]) -> bool:
+ # 双方都很聪明的前提下, 先手必胜无疑
+ # 先手可以提前观察偶数堆还是基数的石头总数更多
+ return True
+```
+
+由[JodyZ0203](https://github.com/JodyZ0203)提供 877. 石子游戏 C++ 解法代码:
+
+```cpp
+class Solution {
+public:
+ bool stoneGame(vector
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+[dekunma](https://www.linkedin.com/in/dekun-ma-036a9b198/) 提供C++代码
+**解法一:遍历(也可以用双指针):**
+```C++
+class Solution {
+public:
+ bool isSubsequence(string s, string t) {
+ // 遍历s
+ for(int i = 0; i < s.size(); i++) {
+ // 找到s[i]字符在t中的位置
+ size_t pos = t.find(s[i]);
+
+ // 如果s[i]字符不在t中,返回false
+ if(pos == std::string::npos) return false;
+ // 如果s[i]在t中,后面就只看pos以后的字串,防止重复查找
+ else t = t.substr(pos + 1);
+ }
+ return true;
+ }
+};
+```
+
+**解法二:二分查找:**
+```C++
+class Solution {
+public:
+ bool isSubsequence(string s, string t) {
+ int m = s.size(), n = t.size();
+ // 对 t 进行预处理
+ vector
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+C++版本:
+```cpp
+ bool isPalindrome(ListNode* head) {
+ if (head == nullptr || head->next == nullptr) //为空或者只有一个节点时,直接判断为true
+ return true;
+ ListNode* slow = head, * fast = head;
+ while (fast != nullptr) {//首先找到中间节点
+ slow = slow->next;
+ fast = fast->next == nullptr? fast->next:fast->next->next; //因为链表长度可能是奇数或偶数,所以需要进行判断
+ }
+
+ ListNode* temp = nullptr,* pre = nullptr;//pre始终保持后续链表的头部,temp节点则作为中间零时替换的节点
+ while (slow != nullptr) {//利用头插法,将当前节点与后续链表断链处理,反转后半部分的链表
+ temp = slow->next;
+ slow->next = pre;//建立连接
+ pre = slow;//pre始终作为后续链表的头部
+ slow = temp;
+ }
+
+ while (head !=nullptr && pre != nullptr) {//同步进行比较
+ if (head->val != pre->val) {//值有不一样的,说明不是回文联表,直接返回false了
+ return false;
+ }
+ head = head->next;//head向下走,直到走到空
+ pre = pre->next;//pre节点也向下走,直到走到空
+ }
+ return true;//到此说明当前链表是回文链表返回true即可
+ }
+
+```
diff --git a/高频面试系列/合法括号判定.md b/高频面试系列/合法括号判定.md
index 6516aa1..c183b51 100644
--- a/高频面试系列/合法括号判定.md
+++ b/高频面试系列/合法括号判定.md
@@ -11,8 +11,8 @@

相关推荐:
- * [回溯算法解题套路框架](https://labuladong.gitbook.io/algo)
- * [Linux shell 的实用小技巧](https://labuladong.gitbook.io/algo)
+ * [回溯算法解题套路框架](https://labuladong.gitbook.io/algo/)
+ * [Linux shell 的实用小技巧](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -104,7 +104,7 @@ char leftOf(char c) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/高频面试系列/如何去除有序数组的重复元素.md b/高频面试系列/如何去除有序数组的重复元素.md
index 1cdec24..4e662a8 100644
--- a/高频面试系列/如何去除有序数组的重复元素.md
+++ b/高频面试系列/如何去除有序数组的重复元素.md
@@ -110,7 +110,7 @@ def deleteDuplicates(self, head: ListNode) -> ListNode:
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/高频面试系列/子集排列组合.md b/高频面试系列/子集排列组合.md
index 04fe3bb..cda2e83 100644
--- a/高频面试系列/子集排列组合.md
+++ b/高频面试系列/子集排列组合.md
@@ -12,8 +12,8 @@

相关推荐:
- * [回溯算法解题套路框架](https://labuladong.gitbook.io/algo)
- * [twoSum问题的核心思想](https://labuladong.gitbook.io/algo)
+ * [回溯算法解题套路框架](https://labuladong.gitbook.io/algo/)
+ * [twoSum问题的核心思想](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -276,12 +276,49 @@ void backtrack(int[] nums, LinkedList
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+[cchromt](https://github.com/cchroot) 提供 Java 代码:
+
+```java
+// 中心扩展算法
+class Solution {
+ public String longestPalindrome(String s) {
+ // 如果字符串长度小于2,则直接返回其本身
+ if (s.length() < 2) {
+ return s;
+ }
+ String res = "";
+ for (int i = 0; i < s.length() - 1; i++) {
+ // 以 s.charAt(i) 为中心的最长回文子串
+ String s1 = palindrome(s, i, i);
+ // 以 s.charAt(i) 和 s.charAt(i+1) 为中心的最长回文子串
+ String s2 = palindrome(s, i, i + 1);
+ res = res.length() > s1.length() ? res : s1;
+ res = res.length() > s2.length() ? res : s2;
+ }
+ return res;
+ }
+
+ public String palindrome(String s, int left, int right) {
+ // 索引未越界的情况下,s.charAt(left) == s.charAt(right) 则继续向两边拓展
+ while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
+ left--;
+ right++;
+ }
+ // 这里要注意,跳出 while 循环时,恰好满足 s.charAt(i) != s.charAt(j),因此截取的的字符串为[left+1, right-1]
+ return s.substring(left + 1, right);
+ }
+}
+```
+
+做完这题,大家可以去看看 [647. 回文子串](https://leetcode-cn.com/problems/palindromic-substrings/) ,也是类似的题目
diff --git a/高频面试系列/水塘抽样.md b/高频面试系列/水塘抽样.md
index fe0dd09..e107ef5 100644
--- a/高频面试系列/水塘抽样.md
+++ b/高频面试系列/水塘抽样.md
@@ -11,8 +11,8 @@

相关推荐:
- * [一文看懂 session 和 cookie](https://labuladong.gitbook.io/algo)
- * [算法就像搭乐高:带你手撸 LFU 算法](https://labuladong.gitbook.io/algo)
+ * [一文看懂 session 和 cookie](https://labuladong.gitbook.io/algo/)
+ * [算法就像搭乐高:带你手撸 LFU 算法](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -140,7 +140,7 @@ $$ -->
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
diff --git a/高频面试系列/消失的元素.md b/高频面试系列/消失的元素.md
index b45bfd2..19433d0 100644
--- a/高频面试系列/消失的元素.md
+++ b/高频面试系列/消失的元素.md
@@ -11,8 +11,8 @@

相关推荐:
- * [学习算法和数据结构的思路指南](https://labuladong.gitbook.io/algo)
- * [回溯算法最佳实践:括号生成](https://labuladong.gitbook.io/algo)
+ * [学习算法和数据结构的思路指南](https://labuladong.gitbook.io/algo/)
+ * [回溯算法最佳实践:括号生成](https://labuladong.gitbook.io/algo/)
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
@@ -89,6 +89,7 @@ int missingNumber(int[] nums) {
for (int x : nums)
sum += x;
return expect - sum;
+}
```
你看,这种解法应该是最简单的,但说实话,我自己也没想到这个解法,而且我去问了几个大佬,他们也没想到这个最简单的思路。相反,如果去问一个初中生,他也许很快就能想到。
@@ -124,7 +125,7 @@ public int missingNumber(int[] nums) {
**_____________**
-**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。
+**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
@@ -133,7 +134,7 @@ public int missingNumber(int[] nums) {
======其他语言代码======
-
+### python
```python
def missingNumber(self, nums: List[int]) -> int:
@@ -160,4 +161,49 @@ def missingNumber(self, nums: List[int]) -> int:
return res
```
-事实上,在python3中不存在整数溢出的问题(只要内存放得下),思路3的优化提升并不大,不过看上去有内味了哈...
\ No newline at end of file
+事实上,在python3中不存在整数溢出的问题(只要内存放得下),思路3的优化提升并不大,不过看上去有内味了哈...
+
+### c++
+
+[happy-yuxuan](https://github.com/happy-yuxuan) 提供 三种方法的 C++ 代码:
+
+```c++
+// 方法:异或元素和索引
+int missingNumber(vector
-======其他语言代码======
\ No newline at end of file
+======其他语言代码======
+
+[zhuli](https://github.com/1097452462 "zhuli")提供的Java代码:
+```java
+class Solution {
+ public int[] findErrorNums(int[] nums) {
+ int n = nums.length;
+ int dup = -1;
+ for (int i = 0; i < n; i++) {
+ // 元素是从 1 开始的
+ int index = Math.abs(nums[i]) - 1;
+ // nums[index] 小于 0 则说明重复访问
+ if (nums[index] < 0)
+ dup = Math.abs(nums[i]);
+ else
+ nums[index] *= -1;
+ }
+ int missing = -1;
+ for (int i = 0; i < n; i++)
+ // nums[i] 大于 0 则说明没有访问
+ if (nums[i] > 0)
+ // 将索引转换成元素
+ missing = i + 1;
+ return new int[]{dup, missing};
+ }
+}
+```