diff --git a/README.md b/README.md index 45995ef16ddb63382f0de126b381e9f37263846b..0a74a829e4a4336ddcab054c0bc78c5e16604d9c 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 的算法小抄

-Website +Website GitHub

@@ -16,6 +16,8 @@ English version repo and Gitbook is on [english branch](https://github.com/labul ![](pictures/souyisou.png) +好消息,《labuladong 的算法小抄》纸质书出版啦!关注公众号查看详情👆 +

@@ -27,27 +29,36 @@ English version repo and Gitbook is on [english branch](https://github.com/labul 只想要答案的话很容易,题目评论区五花八门的答案,动不动就秀 python 一行代码解决,有那么多人点赞。问题是,你去做算法题,是去学习编程语言的奇技淫巧的,还是学习算法思维的呢?你的快乐,到底源自复制别人的一行代码通过测试,已完成题目 +1,还是源自自己通过逻辑推理和算法框架不看答案写出解法? -网上总有大佬喷我,说我写这玩意太基础了,根本没必要啰嗦。我只能说大家刷算法就是找工作吃饭的,不是打竞赛的,我也是一路摸爬滚打过来的,我们要的是清楚明白有所得,不是故弄玄虚无所指。不想办法做到通俗易懂,难道要上来先把《算法导论》吹上天,然后把人家都心怀敬仰地劝退? +网上总有大佬喷我,说我写的东西太基础,要么说不能借助框架思维来学习算法。我只能说大家刷算法就是找工作吃饭的,不是打竞赛的,我也是一路摸爬滚打过来的,我们要的是清楚明白有所得,不是故弄玄虚无所指。 + +不想办法做到通俗易懂,难道要上来先把《算法导论》吹上天,然后把人家都心怀敬仰地劝退? **做啥事情做多了,都能发现套路的,我把各种算法套路框架总结出来,相信可以帮助其他人少走弯路**。我这个纯靠自学的小童鞋,花了一年时间刷题和总结,自己写了一份算法小抄,后面有目录,这里就不废话了。 ### 使用方法 -1、**先给本仓库点个 star,满足一下我的虚荣心**,文章质量绝对值你一个 star。我还在继续创作,给我一点继续写文的动力,感谢。 +**1、先给本仓库点个 star,满足一下我的虚荣心**,文章质量绝对值你一个 star。我还在继续创作,给我一点继续写文的动力,感谢。 + +**2、建议收藏我的在线网站,每篇文章开头都有对应的力扣题目链接,可以边看文章边刷题**: + +Gitbook 地址:https://labuladong.gitbook.io/algo + +GitBook 在国内访问速度很慢,且常被攻击,我特意部署了两个镜像站点,大家可根据网络情况自行选择: + +GitHub Pages 地址:https://labuladong.github.io/algo -2、**建议收藏我的 Gitbook 网站,每篇文章开头都有对应的力扣题目链接,可以边看文章边刷题**: +Gitee Pages 地址:https://labuladong.gitee.io/algo -Gitbook 地址:https://labuladong.gitbook.io/algo/ -3、建议关注我的公众号 **labuladong**,坚持高质量原创,说是最良心最硬核的技术公众号都不为过。本仓库的文章就是从公众号里整理出来的**一部分**内容,公众号后台回复关键词【电子书】可以获得这份小抄的完整版本;回复【加群】可以加入我们的刷题群,和大家一起讨论算法问题,分享内推机会: +**3、建议关注我的公众号 labuladong,坚持高质量原创,说是最良心最硬核的技术公众号都不为过**。本仓库的文章就是从公众号里整理出来的**一部分**内容,公众号可以查看更多内容;公众号后台回复关键词【加群】可以加入我们的刷题群,和大家一起讨论算法问题,分享内推机会:

-4、欢迎关注 [我的知乎](https://www.zhihu.com/people/labuladong)。 +**4、欢迎关注 [我的知乎](https://www.zhihu.com/people/labuladong)**。 -我一直在写优质文章,但是后续的文章只发布到公众号/gitbook/知乎,不能开放到 GitHub。因为本仓库太火了,很多人直接拿我的文章去开付费专栏,价格还不便宜,我这免费写给您看,何必掏冤枉钱呢?所以多多关注本作者,多多宣传,谁也不希望劣币驱逐良币不是么? +我一直在写优质文章,但是后续的文章只发布到公众号/网站/知乎,不能开放到 GitHub。因为本仓库太火了,很多人直接拿我的文章去开付费专栏,价格还不便宜,我这免费写给您看,何必掏冤枉钱呢?所以多多关注本作者,多多宣传,谁也不希望劣币驱逐良币不是么? 其他的先不多说了,直接上干货吧,我们一起搞定 LeetCode,感受一下支配算法的乐趣。 diff --git a/pictures/qrcode.jpg b/pictures/qrcode.jpg index 590f0fd7d538ab858e5e2b97cadae6c8688cce92..83e46cb25f6b9788c224f8beb4619cb9d4f0dad2 100644 Binary files a/pictures/qrcode.jpg and b/pictures/qrcode.jpg differ diff --git "a/\345\207\272\347\211\210\346\216\250\345\271\2771.jpeg" "b/\345\207\272\347\211\210\346\216\250\345\271\2771.jpeg" new file mode 100644 index 0000000000000000000000000000000000000000..6205bfee59f2363a6117d26ac667b50d06cb43cf Binary files /dev/null and "b/\345\207\272\347\211\210\346\216\250\345\271\2771.jpeg" differ diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213KMP\345\255\227\347\254\246\345\214\271\351\205\215\347\256\227\346\263\225.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213KMP\345\255\227\347\254\246\345\214\271\351\205\215\347\256\227\346\263\225.md" index a0cc4cef17ac792186ecfa556ea996fa0ef3c769..37a597be00a9b151d43da1c417ce25ef22ee9c3a 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213KMP\345\255\227\347\254\246\345\214\271\351\205\215\347\256\227\346\263\225.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213KMP\345\255\227\347\254\246\345\214\271\351\205\215\347\256\227\346\263\225.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [经典动态规划:最长公共子序列](https://labuladong.gitbook.io/algo) - * [特殊数据结构:单调栈](https://labuladong.gitbook.io/algo) + * [经典动态规划:最长公共子序列](https://labuladong.gitbook.io/algo/) + * [特殊数据结构:单调栈](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -423,7 +423,7 @@ KMP 算法也就是动态规划那点事,我们的公众号文章目录有一 **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -431,4 +431,40 @@ KMP 算法也就是动态规划那点事,我们的公众号文章目录有一

-======其他语言代码====== \ 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/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\345\215\232\345\274\210\351\227\256\351\242\230.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\345\215\232\345\274\210\351\227\256\351\242\230.md" index f9082d8def88e8632697704931409c8ab0f0659a..c75749ad25a126f4507dc78285dc3fc48a13e084 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\345\215\232\345\274\210\351\227\256\351\242\230.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\345\215\232\345\274\210\351\227\256\351\242\230.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [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/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\345\233\233\351\224\256\351\224\256\347\233\230.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\345\233\233\351\224\256\351\224\256\347\233\230.md" index e2641f5b331478857bdee98022dd7f63273f9461..312cad1f0e711e18e58eff566b37574693dc8d76 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\345\233\233\351\224\256\351\224\256\347\233\230.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\345\233\233\351\224\256\351\224\256\347\233\230.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [如何高效寻找素数](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/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\346\255\243\345\210\231\350\241\250\350\276\276.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\346\255\243\345\210\231\350\241\250\350\276\276.md" index 9190afc3144213b2c99d5926f902f1da882dbc8a..a3d13bd0e7244f6cb463083d777bddf746bfcaa1 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\346\255\243\345\210\231\350\241\250\350\276\276.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\346\255\243\345\210\231\350\241\250\350\276\276.md" @@ -10,8 +10,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [我写了首诗,把滑动窗口算法算法变成了默写题](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/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\350\256\276\350\256\241\357\274\232\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\350\256\276\350\256\241\357\274\232\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index edef47328c02568447653d4c6e287acd0c4867a6..55aeb8dc11bb4d9a839914009e206f4282723303 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\350\256\276\350\256\241\357\274\232\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\350\256\276\350\256\241\357\274\232\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -10,8 +10,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [动态规划设计:最大子数组](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/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\350\257\246\350\247\243\350\277\233\351\230\266.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\350\257\246\350\247\243\350\277\233\351\230\266.md" index f51608e184bdace5d17659778966e09b0e70a506..6ce71faa28cdab56554df72e23019cceec2415c9 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\350\257\246\350\247\243\350\277\233\351\230\266.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\350\257\246\350\247\243\350\277\233\351\230\266.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [经典动态规划:完全背包问题](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/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\233\242\347\201\255\350\202\241\347\245\250\351\227\256\351\242\230.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\233\242\347\201\255\350\202\241\347\245\250\351\227\256\351\242\230.md" index 61f64a36a5587908c7036445dd36f02ca72fe865..f34d173e1861521d09db4b55db2851c0ac7762f3 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\233\242\347\201\255\350\202\241\347\245\250\351\227\256\351\242\230.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\233\242\347\201\255\350\202\241\347\245\250\351\227\256\351\242\230.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [动态规划之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/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\255\220\345\272\217\345\210\227\351\227\256\351\242\230\346\250\241\346\235\277.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\255\220\345\272\217\345\210\227\351\227\256\351\242\230\346\250\241\346\235\277.md" index c7a3d5ac5a1336ca8d692c215a9906f3c1b6595b..1f2570a095a7a9d0e19cff8bb275ddd008f42f60 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\255\220\345\272\217\345\210\227\351\227\256\351\242\230\346\250\241\346\235\277.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\255\220\345\272\217\345\210\227\351\227\256\351\242\230\346\250\241\346\235\277.md" @@ -12,8 +12,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [洗牌算法](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/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\212\242\346\210\277\345\255\220.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\212\242\346\210\277\345\255\220.md" index 081c583ac2748e9bf869248bbdb6095736c8a8f6..f2e254112172834de47c89ca5971c834fffb7642 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\212\242\346\210\277\345\255\220.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\212\242\346\210\277\345\255\220.md" @@ -10,7 +10,7 @@ ![](../pictures/souyisou.png) 相关推荐: - * [动态规划之四键键盘](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/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\234\200\344\274\230\345\255\220\347\273\223\346\236\204.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\234\200\344\274\230\345\255\220\347\273\223\346\236\204.md" index caf68cfe986d3841317cef8c999d856f8b03d02b..808f6a6190f089bb1c44eb68560a2e75aa947c2a 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\234\200\344\274\230\345\255\220\347\273\223\346\236\204.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\234\200\344\274\230\345\255\220\347\273\223\346\236\204.md" @@ -12,8 +12,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [搜索引擎背后的经典数据结构和算法](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/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index bff1721aa8a88a5f9794c960a84ab1cdd5d44f55..1fe1cfbd5542c94925d467bd98d94ecf9e2419f6 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [回溯算法解题套路框架](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/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\347\274\226\350\276\221\350\267\235\347\246\273.md" index 8ba18e447dd8ef22be78d3e88e0f2a0a5050a7dc..5d634fb055522df9353e9c7605e07203c32ea5b6 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [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/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\350\264\252\345\277\203\347\256\227\346\263\225\344\271\213\345\214\272\351\227\264\350\260\203\345\272\246\351\227\256\351\242\230.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\350\264\252\345\277\203\347\256\227\346\263\225\344\271\213\345\214\272\351\227\264\350\260\203\345\272\246\351\227\256\351\242\230.md" index 70e27fc085797e3ba5af090b97e9f30a92f99f4f..268654b4e55904a23dde71120229f949e7880181 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\350\264\252\345\277\203\347\256\227\346\263\225\344\271\213\345\214\272\351\227\264\350\260\203\345\272\246\351\227\256\351\242\230.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\350\264\252\345\277\203\347\256\227\346\263\225\344\271\213\345\214\272\351\227\264\350\260\203\345\272\246\351\227\256\351\242\230.md" @@ -1,4 +1,4 @@ -# 贪心算法之区间调度问题 +# 贪心算法之区间调度问题

@@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [如何判定括号合法性](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) {

-======其他语言代码====== \ No newline at end of file +======其他语言代码====== + +### python +Edwenc 提供 第435题的python3 代码: + +```python +class Solution: + def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: + ### 思路是首先找到不重叠的区间的个数 + ### 然后再用总个数减去不重叠个数 + ### 获得的就是 需要移除的个数 + + # 首先获得区间的个数 为0的话就不用移除 + n = len(intervals) + if n==0: + return 0 + + # 按照每个区间的右端点值进行排序 + sorted_list = sorted( intervals , key=lambda x: x[1] ) + + # 不重叠区间个数至少是1 + count = 1 + + # end是所有不重叠的区间中 最大的右端点 + # end的初始值即是sorted_list[0]的右端点 + end = sorted_list[0][1] + + # 从1开始往后找 因为0在上面已经取过了 + for i in range(1,n): + # start是当前区间左端点值 + start = sorted_list[i][0] + # 如果当前左端点比最大右端点都大了(可能相等) + # 说明两区间不重叠 count+1 再更新end + if start>=end: + count += 1 + end = sorted_list[i][1] + + # 最后返回的是 需要移除的区间个数 + return n-count +``` \ No newline at end of file diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\351\253\230\346\245\274\346\211\224\351\270\241\350\233\213\350\277\233\351\230\266.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\351\253\230\346\245\274\346\211\224\351\270\241\350\233\213\350\277\233\351\230\266.md" index ec5e838a26cf1bf80221bce4e85f47a5ab44f1c3..53ebb0211269ebd71315917af923fe91300623f4 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\351\253\230\346\245\274\346\211\224\351\270\241\350\233\213\350\277\233\351\230\266.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\351\253\230\346\245\274\346\211\224\351\270\241\350\233\213\350\277\233\351\230\266.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [手把手带你刷二叉树(第二期)](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 @@ while (lo < hi) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\351\253\230\346\245\274\346\211\224\351\270\241\350\233\213\351\227\256\351\242\230.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\351\253\230\346\245\274\346\211\224\351\270\241\350\233\213\351\227\256\351\242\230.md" index 68bd9b8655bc4294bec9f3cdb048c88f091a0453..af76a51226413bb4a165e5dab89a20c94642d0b8 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\351\253\230\346\245\274\346\211\224\351\270\241\350\233\213\351\227\256\351\242\230.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\351\253\230\346\245\274\346\211\224\351\270\241\350\233\213\351\227\256\351\242\230.md" @@ -12,8 +12,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [如何判断回文链表](https://labuladong.gitbook.io/algo) - * [SQL进阶技巧](https://labuladong.gitbook.io/algo) + * [如何判断回文链表](https://labuladong.gitbook.io/algo/) + * [SQL进阶技巧](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -243,7 +243,7 @@ def superEggDrop(self, K: int, N: int) -> int: return dp(K, N) ``` -这里就不展开其他解法了,留在下一篇文章 [高楼扔鸡蛋进阶](https://labuladong.gitbook.io/algo) +这里就不展开其他解法了,留在下一篇文章 [高楼扔鸡蛋进阶](https://labuladong.gitbook.io/algo/) 我觉得吧,我们这种解法就够了:找状态,做选择,足够清晰易懂,可流程化,可举一反三。掌握这套框架学有余力的话,再去考虑那些奇技淫巧也不迟。 @@ -253,7 +253,7 @@ def superEggDrop(self, K: int, N: int) -> int: **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\346\212\200\346\234\257/linuxshell.md" "b/\346\212\200\346\234\257/linuxshell.md" index 41e581022ce5f77c146bfa6344d312360159385f..055b0c63543d0da094d5a9fbe6839e30f1b22347 100644 --- "a/\346\212\200\346\234\257/linuxshell.md" +++ "b/\346\212\200\346\234\257/linuxshell.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [状态压缩:对动态规划进行降维打击](https://labuladong.gitbook.io/algo) - * [我用四个命令概括了 Git 的所有套路](https://labuladong.gitbook.io/algo) + * [状态压缩:对动态规划进行降维打击](https://labuladong.gitbook.io/algo/) + * [我用四个命令概括了 Git 的所有套路](https://labuladong.gitbook.io/algo/) **-----------** @@ -149,7 +149,7 @@ $ sudo /home/fdl/bin/connect.sh **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\346\212\200\346\234\257/linux\350\277\233\347\250\213.md" "b/\346\212\200\346\234\257/linux\350\277\233\347\250\213.md" index d0a9d8acd57518fe3a12c88c60ecda613bb309c5..365ed469066ad31be06bdd5b0329f22926a2b0e1 100644 --- "a/\346\212\200\346\234\257/linux\350\277\233\347\250\213.md" +++ "b/\346\212\200\346\234\257/linux\350\277\233\347\250\213.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [一文解决三道区间问题](https://labuladong.gitbook.io/algo) - * [Union-Find算法详解](https://labuladong.gitbook.io/algo) + * [一文解决三道区间问题](https://labuladong.gitbook.io/algo/) + * [Union-Find算法详解](https://labuladong.gitbook.io/algo/) **-----------** @@ -133,7 +133,7 @@ $ cmd1 | cmd2 | cmd3 **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\346\212\200\346\234\257/redis\345\205\245\344\276\265.md" "b/\346\212\200\346\234\257/redis\345\205\245\344\276\265.md" index 36d18f09cf93911b3616bcb69cb5871f9dadb578..8fbedbb0a34f83f5c2f21b751897b354e8a6fa67 100644 --- "a/\346\212\200\346\234\257/redis\345\205\245\344\276\265.md" +++ "b/\346\212\200\346\234\257/redis\345\205\245\344\276\265.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [烧饼排序](https://labuladong.gitbook.io/algo) - * [动态规划之正则表达](https://labuladong.gitbook.io/algo) + * [烧饼排序](https://labuladong.gitbook.io/algo/) + * [动态规划之正则表达](https://labuladong.gitbook.io/algo/) **-----------** @@ -98,7 +98,7 @@ Redis 监听的默认端口是 6379,我们设置它接收网卡 127.0.0.1 的 **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\346\212\200\346\234\257/session\345\222\214cookie.md" "b/\346\212\200\346\234\257/session\345\222\214cookie.md" index 7fb7cb473f86c32bb592651c0402f6caf30c36c2..03ce418d260b76170aff0d753ef53b1a0dcd6adc 100644 --- "a/\346\212\200\346\234\257/session\345\222\214cookie.md" +++ "b/\346\212\200\346\234\257/session\345\222\214cookie.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [如何计算完全二叉树的节点数](https://labuladong.gitbook.io/algo) - * [Linux的进程、线程、文件描述符是什么](https://labuladong.gitbook.io/algo) + * [如何计算完全二叉树的节点数](https://labuladong.gitbook.io/algo/) + * [Linux的进程、线程、文件描述符是什么](https://labuladong.gitbook.io/algo/) **-----------** @@ -147,7 +147,7 @@ https://github.com/astaxie/build-web-application-with-golang **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\346\212\200\346\234\257/\345\234\250\347\272\277\347\273\203\344\271\240\345\271\263\345\217\260.md" "b/\346\212\200\346\234\257/\345\234\250\347\272\277\347\273\203\344\271\240\345\271\263\345\217\260.md" index 28f21ffcfdde1e807cd5d6a164d4a52b593d6367..ba865936cae4748c2aad9cecd0eb577f8ea6c1f8 100644 --- "a/\346\212\200\346\234\257/\345\234\250\347\272\277\347\273\203\344\271\240\345\271\263\345\217\260.md" +++ "b/\346\212\200\346\234\257/\345\234\250\347\272\277\347\273\203\344\271\240\345\271\263\345\217\260.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [区间调度之区间合并问题](https://labuladong.gitbook.io/algo) - * [别再说你不懂Linux内存管理了,10张图给你安排的明明白白](https://labuladong.gitbook.io/algo) + * [区间调度之区间合并问题](https://labuladong.gitbook.io/algo/) + * [别再说你不懂Linux内存管理了,10张图给你安排的明明白白](https://labuladong.gitbook.io/algo/) **-----------** @@ -111,7 +111,7 @@ https://sqlzoo.net/ **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\346\212\200\346\234\257/\345\257\206\347\240\201\346\212\200\346\234\257.md" "b/\346\212\200\346\234\257/\345\257\206\347\240\201\346\212\200\346\234\257.md" index 011352ce09dbe7e980996ec875795282824015dd..9ab3b71ab0fe85f411295d6c1a1f02828e9673b2 100644 --- "a/\346\212\200\346\234\257/\345\257\206\347\240\201\346\212\200\346\234\257.md" +++ "b/\346\212\200\346\234\257/\345\257\206\347\240\201\346\212\200\346\234\257.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [我用四个命令概括了 Git 的所有套路](https://labuladong.gitbook.io/algo) - * [一个方法团灭 nSum 问题](https://labuladong.gitbook.io/algo) + * [我用四个命令概括了 Git 的所有套路](https://labuladong.gitbook.io/algo/) + * [一个方法团灭 nSum 问题](https://labuladong.gitbook.io/algo/) **-----------** @@ -194,7 +194,7 @@ HTTPS 协议中的 SSL/TLS 安全层会组合使用以上几种加密方式,** **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -203,4 +203,4 @@ HTTPS 协议中的 SSL/TLS 安全层会组合使用以上几种加密方式,**

======其他语言代码====== -[test ad](https://labuladong.gitbook.io/algo) \ No newline at end of file +[test ad](https://labuladong.gitbook.io/algo/) \ No newline at end of file diff --git "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\344\272\214\345\217\211\345\240\206\350\257\246\350\247\243\345\256\236\347\216\260\344\274\230\345\205\210\347\272\247\351\230\237\345\210\227.md" "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\344\272\214\345\217\211\345\240\206\350\257\246\350\247\243\345\256\236\347\216\260\344\274\230\345\205\210\347\272\247\351\230\237\345\210\227.md" index 535362546413a8ab51d09088ab686cffcee7fe00..72c98cfe343300165355ba7ef0fe68b52d574bbc 100644 --- "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\344\272\214\345\217\211\345\240\206\350\257\246\350\247\243\345\256\236\347\216\260\344\274\230\345\205\210\347\272\247\351\230\237\345\210\227.md" +++ "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\344\272\214\345\217\211\345\240\206\350\257\246\350\247\243\345\256\236\347\216\260\344\274\230\345\205\210\347\272\247\351\230\237\345\210\227.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [算法就像搭乐高:带你手撸 LRU 算法](https://labuladong.gitbook.io/algo) - * [拆解复杂问题:实现计算器](https://labuladong.gitbook.io/algo) + * [算法就像搭乐高:带你手撸 LRU 算法](https://labuladong.gitbook.io/algo/) + * [拆解复杂问题:实现计算器](https://labuladong.gitbook.io/algo/) **-----------** @@ -231,7 +231,7 @@ public Key delMax() { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\223\215\344\275\234\351\233\206\351\224\246.md" "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\223\215\344\275\234\351\233\206\351\224\246.md" index b30ceebb9b7bed1b1b110d1a03d83ab48ca8a8b1..2e7d63d709c44599536f51b6aefea3baa9913ee8 100644 --- "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\223\215\344\275\234\351\233\206\351\224\246.md" +++ "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\223\215\344\275\234\351\233\206\351\224\246.md" @@ -1,4 +1,4 @@ -# 二叉搜索树操作集锦 +# 二叉搜索树操作集锦

@@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [特殊数据结构:单调队列](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) {

+ ======其他语言代码====== -[dekunma](https://www.linkedin.com/in/dekun-ma-036a9b198/)提供第98题C++代码: -```C++ + +### c++ + +[dekunma](https://www.linkedin.com/in/dekun-ma-036a9b198/)提供第98题C++代码: + +```c++ /** * Definition for a binary tree node. * struct TreeNode { @@ -343,3 +348,143 @@ public: } }; ``` + + +[yanggg1997](https://github.com/yanggg1997)提供第100题C++代码: + +``` c++ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + bool isSameTree(TreeNode* p, TreeNode* q) { + // 若当前节点均为空,则此处相同 + if(!p && !q) return true; + // 若当前节点在一棵树上有而另一棵树上为空,则两棵树不同 + if(!p && q) return false; + if(p && !q) return false; + // 若当前节点在两棵树上均存在。 + if(p->val != q->val) + { + return false; + } + else + { + // 向左右子树分别递归判断 + return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); + } + } +}; +``` + +### python + +[ChenjieXu](https://github.com/ChenjieXu)提供第98题Python3代码: + +```python +def isValidBST(self, root): + # 递归函数 + def helper(node, lower = float('-inf'), upper = float('inf')): + if not node: + return True + + val = node.val + if val <= lower or val >= upper: + return False + # 右节点 + if not helper(node.right, val, upper): + return False + # 左节点 + if not helper(node.left, lower, val): + return False + return True + + return helper(root) + +``` + +[lixiandea](https://github.com/lixiandea)提供第100题Python3代码: + +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: + ''' + 当前节点值相等且树的子树相等,则树相等。 + 递归退出条件:两个节点存在一个节点为空 + ''' + if p == None: + if q == None: + return True + else: + return False + if q == None: + return False + # 当前节点相同且左子树和右子树分别相同 + return p.val==q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) +``` + + +[Edwenc](https://github.com/Edwenc) 提供 leetcode第450题的python3 代码: + +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right + +class Solution: + def deleteNode(self, root: TreeNode, key: int) -> TreeNode: + # 如果没有树 直接返回None + if root == None: + return None + + # 如果要删除的结点 就是当前结点 + if root.val == key: + # 左子树为空 只有右子树需要被更新 直接返回 + if root.left == None: + return root.right + # 右子树为空 只有左子树需要被更新 直接返回 + if root.right== None: + return root.left + + # 找出此结点左子树的最大值 + # 用这个最大值 来代替当前结点 + # 再在左子树中递归地删除这个最大值结点 + big = self.getMax( root.left ) + root.val = big.val + root.left = self.deleteNode( root.left , big.val ) + + # 当前结点较大 它的左子树中需要删除节点 递归到左子树 + elif root.val > key: + root.left = self.deleteNode( root.left , key) + # 当前结点较小 它的右子树中需要删除节点 递归到右子树 + else: + root.right= self.deleteNode( root.right, key) + + return root + + # 辅助函数 + # 功能是找出此二叉搜索树中最大元素的结点 并返回此结点 + def getMax( self , node ): + # 一直找它的右子树 直到为空 + while node.right: + node = node.right + return node +``` diff --git "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\345\215\225\350\260\203\346\240\210.md" "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\345\215\225\350\260\203\346\240\210.md" index 5cbdc4e61f7ef853a8562acc1b14350ebb373eda..b4670bb2688aebc44293f6215f48d5773c54398a 100644 --- "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\345\215\225\350\260\203\346\240\210.md" +++ "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\345\215\225\350\260\203\346\240\210.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: -* [回溯算法解题套路框架](https://labuladong.gitbook.io/algo) - * [动态规划解题套路框架](https://labuladong.gitbook.io/algo) +* [回溯算法解题套路框架](https://labuladong.gitbook.io/algo/) + * [动态规划解题套路框架](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -173,7 +173,7 @@ vector nextGreaterElements(vector& nums) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -181,4 +181,117 @@ vector nextGreaterElements(vector& nums) {

-======其他语言代码====== \ 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 stack = new Stack <>(); + HashMap map = new HashMap <>(); + int[] result = new int[nums1.length]; + for (int value : nums2) { + while (!stack.empty() && value > stack.peek()) { + map.put(stack.pop(), value); + } + stack.push(value); + } + while (!stack.empty()) { + map.put(stack.pop(), -1); + } + for (int i = 0; i < nums1.length; i++) { + result[i] = map.get(nums1[i]); + } + return result; +} +``` + +[ZakAnun](https://github.com/ZakAnun) 提供代码 + +```java +// 739. Daily Temperatures +class Solution { + public int[] dailyTemperatures(int[] T) { + Stack stack = new Stack<>(); + int[] ans = new int[T.length]; + for (int i = 0; i < T.length; i++) { + // 如果压栈之后不满足单调递减,弹出元素,直至保持单调性 + while (!stack.isEmpty() && T[i] > T[stack.peek()]) { + int index = stack.pop(); + // 被弹出的元素(T[index])都是小于当前的元素(T[i]),由于栈内元素单调递减,大于被弹出元素(index)的最近的就是当前元素(i) + ans[index] = i - index; + } + stack.push(i); + } + return ans; + } +} +``` + +[JiangangZhao](https://github.com/JiangangZhao)提供【503.下一个更大元素II】【java】 + +```java +class Solution { + public int[] nextGreaterElements(int[] nums) { + //数组长度 + int n = nums.length; + //逻辑拼接,数组长度翻倍 + int len = n*2 - 1; + //存储结果数组 + int[] res = new int[n]; + //存放索引,不是元素 + LinkedList s = new LinkedList<>(); + //从前往后遍历 + for (int i = 0; i < len; ++i) { + //索引要取模 + int val = nums[i % n]; + //当前元素比栈顶元素大,即是栈顶元素的下一个更大的元素 + while (!s.isEmpty() && val > nums[s.peek()]) { + res[s.pop()] = val; + } + //i maxSlidingWindow(vector& nums, int k) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -210,4 +210,105 @@ vector maxSlidingWindow(vector& nums, int k) {

-======其他语言代码====== \ 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/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\345\256\236\347\216\260\350\256\241\347\256\227\345\231\250.md" "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\345\256\236\347\216\260\350\256\241\347\256\227\345\231\250.md" index 2b980f745e111728c0c47ffa02a43c838eb272f5..384a86f92a5eade5aa1db36660de010dad3e9b03 100644 --- "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\345\256\236\347\216\260\350\256\241\347\256\227\345\231\250.md" +++ "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\345\256\236\347\216\260\350\256\241\347\256\227\345\231\250.md" @@ -12,8 +12,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [特殊数据结构:单调队列](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/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\350\256\276\350\256\241Twitter.md" "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\350\256\276\350\256\241Twitter.md" index 399fbbbd2820c05fa4d4dcac7db32e458cf9d992..96db45541c5c4b60f8fca783d8eb114709a736b5 100644 --- "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\350\256\276\350\256\241Twitter.md" +++ "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\350\256\276\350\256\241Twitter.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [面试官:你说对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 followed; // 用户关注了那些人 + User(int userId) { + this->id = userId; + head = nullptr; + // 要先把自己关注了 + followed.insert(id); + } + void follow(int userId) { + followed.insert(userId); + } + void unfollow(int userId) { + // 不可以取关自己 + if (userId != this->id) + followed.erase(userId); + } + void post(int contentId) { + Tweet *twt = new Tweet(contentId, timestamp); + timestamp++; + // 将新建的推文插入链表头 + // 越靠前的推文 timestamp 值越大 + twt->next = head; + head = twt; + } +}; +class Twitter { +private: + // 映射将 userId 和 User 对象对应起来 + unordered_map userMap; + // 判断该用户存不存在系统中,即userMap中存不存在id + inline bool contain(int id) { + return userMap.find(id) != userMap.end(); + } +public: + Twitter() { + userMap.clear(); + } + /* user 发表一条 tweet 动态 */ + void postTweet(int userId, int tweetId) { + if (!contain(userId)) + userMap[userId] = new User(userId); + userMap[userId]->post(tweetId); + } + /* 返回该 user 关注的人(包括他自己)最近的动态 id, + 最多 10 条,而且这些动态必须按从新到旧的时间线顺序排列。*/ + vector getNewsFeed(int userId) { + vector ret; + if (!contain(userId)) return ret; + // 构造一个自动通过Tweet发布的time属性从大到小排序的二叉堆 + typedef function Compare; + Compare cmp = [](const Tweet *a, const Tweet *b) { + return a->getTime() < b->getTime(); + }; + priority_queue, Compare> q(cmp); + // 关注列表的用户Id + unordered_set &users = userMap[userId]->followed; + // 先将所有链表头节点插入优先级队列 + for (int id : users) { + if (!contain(id)) continue; + Tweet *twt = userMap[id]->head; + if (twt == nullptr) continue; + q.push(twt); + } + while (!q.empty()) { + Tweet *t = q.top(); q.pop(); + ret.push_back(t->getId()); + if (ret.size() == 10) return ret; // 最多返回 10 条就够了 + if (t->next) + q.push(t->next); + } + return ret; + } + /* follower 关注 followee */ + void follow(int followerId, int followeeId) { + // 若 follower 不存在,则新建 + if (!contain(followerId)) + userMap[followerId] = new User(followerId); + // 若 followee 不存在,则新建 + if (!contain(followeeId)) + userMap[followeeId] = new User(followeeId); + userMap[followerId]->follow(followeeId); + } + /* follower 取关 followee,如果 Id 不存在则什么都不做 */ + void unfollow(int followerId, int followeeId) { + if (contain(followerId)) + userMap[followerId]->unfollow(followeeId); + } +}; +``` \ No newline at end of file diff --git "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\351\200\222\345\275\222\345\217\215\350\275\254\351\223\276\350\241\250\347\232\204\344\270\200\351\203\250\345\210\206.md" "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\351\200\222\345\275\222\345\217\215\350\275\254\351\223\276\350\241\250\347\232\204\344\270\200\351\203\250\345\210\206.md" index 830e50372aa2c2595bf0e5a2e65860ecfb8628cd..eef6accb1a94a96417a50c9b01e5dda4ea255415 100644 --- "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\351\200\222\345\275\222\345\217\215\350\275\254\351\223\276\350\241\250\347\232\204\344\270\200\351\203\250\345\210\206.md" +++ "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\351\200\222\345\275\222\345\217\215\350\275\254\351\223\276\350\241\250\347\232\204\344\270\200\351\203\250\345\210\206.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [特殊数据结构:单调队列](https://labuladong.gitbook.io/algo) - * [回溯算法最佳实践:括号生成](https://labuladong.gitbook.io/algo) + * [特殊数据结构:单调队列](https://labuladong.gitbook.io/algo/) + * [回溯算法最佳实践:括号生成](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -210,7 +210,7 @@ ListNode reverseBetween(ListNode head, int m, int n) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -218,4 +218,88 @@ ListNode reverseBetween(ListNode head, int m, int n) {

-======其他语言代码====== \ 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;inext; + //获取待反转节点的第一个节点 + head=pre->next; + //迭代反转n-m次,将head的next节点移动到需要反转链表部分的首部 + for(int i=m;inext; + head->next=t->next; + t->next=pre->next; + pre->next=t; + } + //返回哨兵节点 + return dummy->next; + } +}; +``` + +### python +[DiamondI](https://github.com/DiamondI) 提供python3版本代码: + +思路:递归。时间复杂度为O(n),由于递归调用需要借助栈的空间,因此空间复杂度亦为O(n)。 + +```python3 +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def __init__(self): + self.__successor = None + + def __reverseN(self, head: ListNode, n: int) -> ListNode: + if n == 1: + # 记录第 n + 1 个节点 + self.__successor = head.next; + return head; + # 以 head.next 为起点,需要反转前 n - 1 个节点 + last = self.__reverseN(head.next, n - 1); + + head.next.next = head; + # 让反转之后的 head 节点和后面的节点连起来 + head.next = self.__successor; + return last; + + def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: + # base case + if m == 1: + return self.__reverseN(head, n); + # 前进到反转的起点触发 base case + head.next = self.reverseBetween(head.next, m - 1, n - 1); + return head; +``` diff --git "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" index 20b6a8e54d25d46c7b2a1d9015e7028ad41ca3bb..a9d34465f37ae602943848a02ad0b3aededcaf2c 100644 --- "a/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" +++ "b/\346\225\260\346\215\256\347\273\223\346\236\204\347\263\273\345\210\227/\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [手把手带你刷二叉树(第三期)](https://labuladong.gitbook.io/algo) - * [高性能短链设计](https://labuladong.gitbook.io/algo) + * [手把手带你刷二叉树(第三期)](https://labuladong.gitbook.io/algo/) + * [高性能短链设计](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -222,7 +222,7 @@ public boolean empty() { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/FloodFill\347\256\227\346\263\225\350\257\246\350\247\243\345\217\212\345\272\224\347\224\250.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/FloodFill\347\256\227\346\263\225\350\257\246\350\247\243\345\217\212\345\272\224\347\224\250.md" index 1cf2dbf2b82741e2e02920e75c960ec803285ca5..28b0fa5a4b4e4c46c773c3612184709fa9963645 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/FloodFill\347\256\227\346\263\225\350\257\246\350\247\243\345\217\212\345\272\224\347\224\250.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/FloodFill\347\256\227\346\263\225\350\257\246\350\247\243\345\217\212\345\272\224\347\224\250.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [如何高效进行模幂运算](https://labuladong.gitbook.io/algo) - * [经典动态规划:0-1 背包问题](https://labuladong.gitbook.io/algo) + * [如何高效进行模幂运算](https://labuladong.gitbook.io/algo/) + * [经典动态规划:0-1 背包问题](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -126,7 +126,7 @@ image[x][y] = newColor; 完全 OK,这也是处理「图」的一种常用手段。不过对于此题,不用开数组,我们有一种更好的方法,那就是回溯算法。 -前文 [回溯算法框架套路](https://labuladong.gitbook.io/algo)讲过,这里不再赘述,直接套回溯算法框架: +前文 [回溯算法框架套路](https://labuladong.gitbook.io/algo/)讲过,这里不再赘述,直接套回溯算法框架: ```java void fill(int[][] image, int x, int y, @@ -231,7 +231,7 @@ int fill(int[][] image, int x, int y, **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/UnionFind\347\256\227\346\263\225\345\272\224\347\224\250.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/UnionFind\347\256\227\346\263\225\345\272\224\347\224\250.md" index fef484a3efe15aa544db9dde28f070b5fd6deb9a..1217326aed926d8c44eecf2c96d6f2628260f01b 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/UnionFind\347\256\227\346\263\225\345\272\224\347\224\250.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/UnionFind\347\256\227\346\263\225\345\272\224\347\224\250.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [手把手带你刷二叉树(第一期)](https://labuladong.gitbook.io/algo) - * [二分查找详解](https://labuladong.gitbook.io/algo) + * [手把手带你刷二叉树(第一期)](https://labuladong.gitbook.io/algo/) + * [二分查找详解](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -20,6 +20,8 @@ [990.等式方程的可满足性](https://leetcode-cn.com/problems/surrounded-regions) +[261.以图判树](https://leetcode-cn.com/problems/graph-valid-tree/) + **-----------** 上篇文章很多读者对于 Union-Find 算法的应用表示很感兴趣,这篇文章就拿几道 LeetCode 题目来讲讲这个算法的巧妙用法。 @@ -242,7 +244,7 @@ boolean equationsPossible(String[] equations) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -250,4 +252,78 @@ boolean equationsPossible(String[] equations) {

-======其他语言代码====== \ 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/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/UnionFind\347\256\227\346\263\225\350\257\246\350\247\243.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/UnionFind\347\256\227\346\263\225\350\257\246\350\247\243.md" index 977d8baaf5f11f955e557dce2cea006ea6e6a488..32e37de8aa5c7972d616d3554ac59791412c67bf 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/UnionFind\347\256\227\346\263\225\350\257\246\350\247\243.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/UnionFind\347\256\227\346\263\225\350\257\246\350\247\243.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [一文秒杀四道原地修改数组的算法题](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/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/twoSum\351\227\256\351\242\230\347\232\204\346\240\270\345\277\203\346\200\235\346\203\263.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/twoSum\351\227\256\351\242\230\347\232\204\346\240\270\345\277\203\346\200\235\346\203\263.md" index 327fc2313cc33e78f1462adf056025424d3473d3..a1a92c5546d436f29969f7fc5a0bdb0b73a48761 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/twoSum\351\227\256\351\242\230\347\232\204\346\240\270\345\277\203\346\200\235\346\203\263.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/twoSum\351\227\256\351\242\230\347\232\204\346\240\270\345\277\203\346\200\235\346\203\263.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [我写了首诗,让你闭着眼睛也能写对二分搜索](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/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\344\270\272\344\273\200\344\271\210\346\216\250\350\215\220\347\256\227\346\263\2254.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\344\270\272\344\273\200\344\271\210\346\216\250\350\215\220\347\256\227\346\263\2254.md" index 8792b9cb3b6df3f726c6bf26821789990f4a8138..ff2432627fe8401b63e1e335769a48d05489e8f1 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\344\270\272\344\273\200\344\271\210\346\216\250\350\215\220\347\256\227\346\263\2254.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\344\270\272\344\273\200\344\271\210\346\216\250\350\215\220\347\256\227\346\263\2254.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [递归反转链表的一部分](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/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\344\272\214\345\210\206\346\237\245\346\211\276\350\257\246\350\247\243.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\344\272\214\345\210\206\346\237\245\346\211\276\350\257\246\350\247\243.md" index a413541bfba6a4a88cb23d829441670a8d7a7fdb..49f8f7f73d049d8c19dfcb3b4415182c29e5853f 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\344\272\214\345\210\206\346\237\245\346\211\276\350\257\246\350\247\243.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\344\272\214\345\210\206\346\237\245\346\211\276\350\257\246\350\247\243.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [手把手带你刷二叉树(第三期)](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/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\344\277\241\345\260\201\345\265\214\345\245\227\351\227\256\351\242\230.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\344\277\241\345\260\201\345\265\214\345\245\227\351\227\256\351\242\230.md" index bd6997f2b22362881aacf7f900173e7d743abfac..a31453cd66fd7bff0d1996e28d3cd627e0835c66 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\344\277\241\345\260\201\345\265\214\345\245\227\351\227\256\351\242\230.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\344\277\241\345\260\201\345\265\214\345\245\227\351\227\256\351\242\230.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [讲两道常考的阶乘算法题](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/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\207\240\344\270\252\345\217\215\347\233\264\350\247\211\347\232\204\346\246\202\347\216\207\351\227\256\351\242\230.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\207\240\344\270\252\345\217\215\347\233\264\350\247\211\347\232\204\346\246\202\347\216\207\351\227\256\351\242\230.md" index 767a11f184c11d5a4b548a5c31233a0fdde5e120..5a3adaee3f7d96888b8017187ac31a315343123c 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\207\240\344\270\252\345\217\215\347\233\264\350\247\211\347\232\204\346\246\202\347\216\207\351\227\256\351\242\230.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\207\240\344\270\252\345\217\215\347\233\264\350\247\211\347\232\204\346\246\202\347\216\207\351\227\256\351\242\230.md" @@ -11,12 +11,12 @@ ![](../pictures/souyisou.png) 相关推荐: - * [学习算法和数据结构的思路指南](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/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\211\215\347\274\200\345\222\214\346\212\200\345\267\247.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\211\215\347\274\200\345\222\214\346\212\200\345\267\247.md" index ef9698648a61a1fc1d1d1885e7656c183c2e5e6e..5a3d3ce1c5c145106dfaab238b6a0a777c83ed98 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\211\215\347\274\200\345\222\214\346\212\200\345\267\247.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\211\215\347\274\200\345\222\214\346\212\200\345\267\247.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [如何去除有序数组的重复元素](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/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\214\272\351\227\264\344\272\244\351\233\206\351\227\256\351\242\230.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\214\272\351\227\264\344\272\244\351\233\206\351\227\256\351\242\230.md" index b2ca23838f36602a1c0fffa2364d3ba5590e66ab..b84e324a02bd6beb711330887d3c7029566d4704 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\214\272\351\227\264\344\272\244\351\233\206\351\227\256\351\242\230.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\214\272\351\227\264\344\272\244\351\233\206\351\227\256\351\242\230.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [经典动态规划:编辑距离](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 res = new ArrayList<>(); + int a = 0, b = 0; + while(a < A.length && b < B.length) { + // 确定左边界,两个区间左边界的最大值 + int left = Math.max(A[a][0], B[b][0]); + // 确定右边界,两个区间右边界的最小值 + int right = Math.min(A[a][1], B[b][1]); + // 左边界小于右边界则加入结果集 + if (left <= right) + res.add(new int[] {left, right}); + // 右边界更大的保持不动,另一个指针移动,继续比较 + if(A[a][1] < B[b][1]) a++; + else b++; + } + // 将结果转为数组 + return res.toArray(new int[0][]); + } +} +``` \ No newline at end of file diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\214\272\351\227\264\350\260\203\345\272\246\351\227\256\351\242\230\344\271\213\345\214\272\351\227\264\345\220\210\345\271\266.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\214\272\351\227\264\350\260\203\345\272\246\351\227\256\351\242\230\344\271\213\345\214\272\351\227\264\345\220\210\345\271\266.md" index e4ca2dc2f09449ff749f54582ec58c093ca3458e..37db115b96908756f54ac171780f4307ff1355b0 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\214\272\351\227\264\350\260\203\345\272\246\351\227\256\351\242\230\344\271\213\345\214\272\351\227\264\345\220\210\345\271\266.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\214\272\351\227\264\350\260\203\345\272\246\351\227\256\351\242\230\344\271\213\345\214\272\351\227\264\345\220\210\345\271\266.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [Git原理之最近公共祖先](https://labuladong.gitbook.io/algo) - * [洗牌算法](https://labuladong.gitbook.io/algo) + * [Git原理之最近公共祖先](https://labuladong.gitbook.io/algo/) + * [洗牌算法](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -83,12 +83,110 @@ def merge(intervals): **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。

+======其他语言代码====== -======其他语言代码====== \ No newline at end of file +### java + +```java +class Solution { + /** + * 1. 先对区间集合进行排序(根据开始位置) + * 2. 合并的情况一共有三种 + * a. b. c. + * |---------| |--------| |--------| + * |---------| |--| |--------| + * a和b两种情况,合并取右边界大的值,c情况不合并 + * + */ + + private int[][] tmp; + + public int[][] merge(int[][] intervals) { + if(intervals == null ||intervals.length == 0)return new int[0][0]; + int length = intervals.length; + //将列表中的区间按照左端点升序排序 + // Arrays.sort(intervals,(v1,v2) -> v1[0]-v2[0]); + + this.tmp = new int[length][2]; + sort(intervals,0,length-1); + + int[][] ans = new int[length][2]; + int index = -1; + for(int[] interval:intervals){ + // 当结果数组是空是,或者当前区间的起始位置 > 结果数组中最后区间的终止位置(即上图情况c); + // 则不合并,直接将当前区间加入结果数组。 + if(index == -1 || interval[0] > ans[index][1]){ + ans[++index] = interval; + }else{ + // 反之将当前区间合并至结果数组的最后区间(即上图情况a,b) + ans[index][1] = Math.max(ans[index][1],interval[1]); + } + } + return Arrays.copyOf(ans, index + 1); + } + + //归并排序 + public void sort(int[][] intervals,int l,int r){ + if(l >= r)return; + + int mid = l + (r-l)/2; + sort(intervals,l,mid); + sort(intervals,mid+1,r); + + //合并 + int i=l,j=mid+1; + for(int k=l;k<=r;k++){ + if(i>mid)tmp[k]=intervals[j++]; + else if(j>r)tmp[k]=intervals[i++]; + else if(intervals[i][0]>intervals[j][0])tmp[k] = intervals[j++]; + else tmp[k] = intervals[i++]; + } + + System.arraycopy(tmp,l,intervals,l,r-l+1); + } + +} +``` + +### c++ + +[Kian](https://github.com/KianKw/) 提供第 56 题 C++ 代码 + +```c++ +class Solution { +public: + vector> merge(vector>& intervals) { + // len 为 intervals 的长度 + int len = intervals.size(); + if (len < 1) + return {}; + + // 按区间的 start 升序排列 + sort(intervals.begin(), intervals.end()); + + // 初始化 res 数组 + vector> res; + res.push_back(intervals[0]); + + for (int i = 1; i < len; i++) { + vector curr = intervals[i]; + // res.back() 为 res 中最后一个元素的索引 + if (curr[0] <= res.back()[1]) { + // 找到最大的 end + res.back()[1] = max(res.back()[1], curr[1]); + } else { + // 处理下一个待合并区间 + res.push_back(curr); + } + } + return res; + } +}; +``` diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" index 15531f67eeeffdfbdb5387a973e3300520c55017..c77dc4190929fb487e08c71f5e788c9ec0139efa 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [一文秒杀四道原地修改数组的算法题](https://labuladong.gitbook.io/algo) - * [Linux的进程、线程、文件描述符是什么](https://labuladong.gitbook.io/algo) + * [一文秒杀四道原地修改数组的算法题](https://labuladong.gitbook.io/algo/) + * [Linux的进程、线程、文件描述符是什么](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -221,13 +221,13 @@ void reverse(int[] nums) { 这也许是双指针技巧的最高境界了,如果掌握了此算法,可以解决一大类子字符串匹配的问题,不过「滑动窗口」稍微比上述的这些算法复杂些。 -幸运的是,这类算法是有框架模板的,而且[这篇文章](https://labuladong.gitbook.io/algo)就讲解了「滑动窗口」算法模板,帮大家秒杀几道 LeetCode 子串匹配的问题。 +幸运的是,这类算法是有框架模板的,而且[这篇文章](https://labuladong.gitbook.io/algo/)就讲解了「滑动窗口」算法模板,帮大家秒杀几道 LeetCode 子串匹配的问题。 **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -237,26 +237,7 @@ void reverse(int[] nums) { ======其他语言代码====== -[ryandeng32](https://github.com/ryandeng32/) 提供 Python 代码 -```python -class Solution: - def hasCycle(self, head: ListNode) -> bool: - # 检查链表头是否为None,是的话则不可能为环形 - if head is None: - return False - # 快慢指针初始化 - slow = fast = head - # 若链表非环形则快指针终究会遇到None,然后退出循环 - while fast.next and fast.next.next: - # 更新快慢指针 - slow = slow.next - fast = fast.next.next - # 快指针追上慢指针则链表为环形 - if slow == fast: - return True - # 退出循环,则链表有结束,不可能为环形 - return False -``` +### java [zhengpj95](https://github.com/zhengpj95) 提供 Java 代码 @@ -280,6 +261,31 @@ public class Solution { } ``` +### c++ + +[deardeer7](https://github.com/DearDeer7/) 提供 C++ 代码 +```cpp +class Solution { +public: + bool hasCycle(ListNode *head) { + // 链表为空或有一个元素,则无环 + if(!head || !head->next) return false; + + ListNode* slow = head; + ListNode* fast = head->next; + + while(fast && fast->next) { + fast = fast->next->next; + slow = slow->next; + // 快慢指针相遇,则有环 + if(fast == slow) return true; + } + return false; // 链表走完,快慢指针未相遇,则无环 + } +}; +``` + + [zhengpj95](https://github.com/zhengpj95) 提供 【2、已知链表中含有环,返回这个环的起始位置】 C++ 代码 > 其实快慢指针问题,也就是著名的 *[Floyd's cycle detection algorithm](https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_Tortoise_and_Hare)* 问题。 @@ -317,3 +323,43 @@ public: } }; ``` + +### python + +[MarineJoker](https://github.com/MarineJoker) 提供 167.两数之和 II - 输入有序数组 Python 代码 +```python +class Solution: + def twoSum(self, numbers: List[int], target: int) -> List[int]: + left, right = 0, len(numbers) - 1 + while left < right: + two_sum = numbers[left] + numbers[right] + if two_sum > target: + right -= 1 # 使得two_sum变小 + elif two_sum < target: + left += 1 # 使得two_sum变大 + elif two_sum == target: + return [left+1, right+1] # 由于索引由1开始 + return [-1, -1] +``` + +[ryandeng32](https://github.com/ryandeng32/) 提供 Python 代码 +```python +class Solution: + def hasCycle(self, head: ListNode) -> bool: + # 检查链表头是否为None,是的话则不可能为环形 + if head is None: + return False + # 快慢指针初始化 + slow = fast = head + # 若链表非环形则快指针终究会遇到None,然后退出循环 + while fast.next and fast.next.next: + # 更新快慢指针 + slow = slow.next + fast = fast.next.next + # 快指针追上慢指针则链表为环形 + if slow == fast: + return True + # 退出循环,则链表有结束,不可能为环形 + return False +``` + diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\233\236\346\272\257\347\256\227\346\263\225\350\257\246\350\247\243\344\277\256\350\256\242\347\211\210.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\233\236\346\272\257\347\256\227\346\263\225\350\257\246\350\247\243\344\277\256\350\256\242\347\211\210.md" index 07e1bfba7d166bd5e9993d6a166e643d7eefac32..034a9fe2d2557bece69938a20d770ccdef3f7262 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\233\236\346\272\257\347\256\227\346\263\225\350\257\246\350\247\243\344\277\256\350\256\242\347\211\210.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\233\236\346\272\257\347\256\227\346\263\225\350\257\246\350\247\243\344\277\256\350\256\242\347\211\210.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [我写了首诗,把滑动窗口算法算法变成了默写题](https://labuladong.gitbook.io/algo) - * [经典动态规划:高楼扔鸡蛋(进阶)](https://labuladong.gitbook.io/algo) + * [我写了首诗,把滑动窗口算法算法变成了默写题](https://labuladong.gitbook.io/algo/) + * [经典动态规划:高楼扔鸡蛋(进阶)](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -301,7 +301,7 @@ def backtrack(...): **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -309,4 +309,67 @@ def backtrack(...):

-======其他语言代码====== \ No newline at end of file +======其他语言代码====== + +由[kepler-zc](https://github.com/kepler-zc) 提供 51.N皇后 Java 解法代码: +```java +class solution { + private List> res = new ArrayList<>(); + + // 输入棋盘边长 n,返回所有合法的放置 + public List> solveNQueens(int n){ + // '.'表示空,'Q'表示皇后,初始化空棋盘 + char[][] chess = new char[n][n]; + for (int i = 0; i < n; i++) { + Arrays.fill(chess[i], '.'); + } + // 已经不能放置皇后的列(被占用) + boolean[] usedCol = new boolean[n]; + // 已经不能放置皇后的正斜线 , 按右上角到左下角排列 , 共2n-1条 + boolean[] usedSlash = new boolean[2*n-1]; + // 已经不能放置皇后的反斜线 , 按左上角到右下角排列 , 共2n-1条 + boolean[] usedBackSlash = new boolean[2*n-1]; + backtrack(chess, 0, usedCol, usedSlash, usedBackSlash); + return res; + } + + // 路径:chess 中小于 row 的那些行都已经成功放置了皇后 + // 选择列表:第 row 行的所有列都是放置皇后的选择 + // 结束条件:row 超过 棋盘最后一行 + private void backtrack(char[][] chess, int row, boolean[] usedCol, boolean[] usedSlash, boolean[] usedBackSlash) { + // 触发结束条件 + if (row == chess.length){ + res.add(construct(chess)); + return; + } + for (int col = 0; col < chess.length; col++) { + // 对合法选择进行回溯操作 + // 分别检查列,左上方, 右上方是否存在皇后冲突,都不冲突集为合法选择。 + if (!usedCol[col] && !usedSlash[row-col+usedCol.length-1] && !usedBackSlash[col+row]){ + // 做选择 + chess[row][col] = 'Q'; + usedCol[col] = true; + // 对坐标为[i,j]的点对应的正斜线和反斜线的索引分别为:row-col+n-1; col+row + usedSlash[row-col+chess.length-1] = true; + usedBackSlash[col+row] = true; + // 进入下一行 + backtrack(chess, row+1, usedCol,usedSlash, usedBackSlash); + // 撤销选择 + chess[row][col] = '.'; + usedCol[col] = false; + usedSlash[row-col+chess.length-1] = false; + usedBackSlash[col+row] = false; + } + } + } + + private List construct(char[][] chess) { + // 数组转List + List path = new ArrayList<>(); + for (char[] chars : chess) { + path.add(new String(chars)); + } + return path; + } +} +``` \ No newline at end of file diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\255\227\347\254\246\344\270\262\344\271\230\346\263\225.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\255\227\347\254\246\344\270\262\344\271\230\346\263\225.md" index 02737442826e16336e4497a5b2d76ad77aa9ccef..966aafd5ec10231b3ed632252f05fa0c7b0058af 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\255\227\347\254\246\344\270\262\344\271\230\346\263\225.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\255\227\347\254\246\344\270\262\344\271\230\346\263\225.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [关于 Linux shell 你必须知道的](https://labuladong.gitbook.io/algo) - * [回溯算法解题套路框架](https://labuladong.gitbook.io/algo) + * [关于 Linux shell 你必须知道的](https://labuladong.gitbook.io/algo/) + * [回溯算法解题套路框架](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -93,7 +93,7 @@ string multiply(string num1, string num2) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -101,4 +101,80 @@ string multiply(string num1, string num2) {

-======其他语言代码====== \ 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/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\255\246\344\271\240\346\225\260\346\215\256\347\273\223\346\236\204\345\222\214\347\256\227\346\263\225\347\232\204\351\253\230\346\225\210\346\226\271\346\263\225.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\255\246\344\271\240\346\225\260\346\215\256\347\273\223\346\236\204\345\222\214\347\256\227\346\263\225\347\232\204\351\253\230\346\225\210\346\226\271\346\263\225.md" index a5beaf3fbe476153bac2b600af49d263c0624bf1..54c0b8d2f37a82929dc55ffc6dbd0b1465935b45 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\255\246\344\271\240\346\225\260\346\215\256\347\273\223\346\236\204\345\222\214\347\256\227\346\263\225\347\232\204\351\253\230\346\225\210\346\226\271\346\263\225.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\255\246\344\271\240\346\225\260\346\215\256\347\273\223\346\236\204\345\222\214\347\256\227\346\263\225\347\232\204\351\253\230\346\225\210\346\226\271\346\263\225.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [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 叉树: ![](../pictures/动态规划详解进阶/5.jpg) @@ -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/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\270\270\347\224\250\347\232\204\344\275\215\346\223\215\344\275\234.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\270\270\347\224\250\347\232\204\344\275\215\346\223\215\344\275\234.md" index f5b577198546922ed0d55c61f32e37ef2536b3d0..b56e56c316babb4d52ffc9255e9a2eda04ce9383 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\270\270\347\224\250\347\232\204\344\275\215\346\223\215\344\275\234.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\270\270\347\224\250\347\232\204\344\275\215\346\223\215\344\275\234.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [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/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\346\264\227\347\211\214\347\256\227\346\263\225.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\346\264\227\347\211\214\347\256\227\346\263\225.md" index 463a7f183e0f2cb8c5580d62a525353f5a4821d8..0ce3b64cf7fe17f23ba9523d162df28888a5aa5b 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\346\264\227\347\211\214\347\256\227\346\263\225.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\346\264\227\347\211\214\347\256\227\346\263\225.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [二叉搜索树操作集锦](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/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\346\273\221\345\212\250\347\252\227\345\217\243\346\212\200\345\267\247.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\346\273\221\345\212\250\347\252\227\345\217\243\346\212\200\345\267\247.md" index 67e6539ca1fbb5dc1f7c7e3ac80d7a8c297c074c..c116188afd1e4d772594a1dbd6b26d4e29536a5b 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\346\273\221\345\212\250\347\252\227\345\217\243\346\212\200\345\267\247.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\346\273\221\345\212\250\347\252\227\345\217\243\346\212\200\345\267\247.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/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\347\203\247\351\245\274\346\216\222\345\272\217.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\347\203\247\351\245\274\346\216\222\345\272\217.md" index 513cbdca8c165141fc2532c5b9b07dc8857b55ca..22ed46bea93a17e20867fa9c37bfa8492ff3d3bb 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\347\203\247\351\245\274\346\216\222\345\272\217.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\347\203\247\351\245\274\346\216\222\345\272\217.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [手把手带你刷二叉树(第三期)](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 @@ ![](../pictures/pancakeSort/title.png) -如何解决这个问题呢?其实类似上篇文章 [递归反转链表的一部分](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 pancakeSort(vector& arr) { + sort(arr, arr.size()); + return res; + } + +private: + // 记录反转操作序列 + vector res; + void sort(vector& cakes, int n){ + // base case + if(n == 1) return; + + // 寻找最大饼的索引 + int maxCakeIndex = max_element(cakes.begin(), cakes.begin() + n) - cakes.begin(); + + // 下面进行把最大的饼放到最后的两次翻转 + // 如果最后一个饼就是最大的, 就不需要翻转, 直接进行下次递归 + if (maxCakeIndex == n-1){ + sort(cakes, n - 1); + return; + } + + // 第一次翻转, 将最大饼翻到最上面 + // 如果第一个饼本来就是最大的, 就不需要第一次翻转. + if (maxCakeIndex != 0) { + reverse(cakes.begin(), cakes.begin() + maxCakeIndex + 1); + res.push_back(maxCakeIndex + 1); + } + + // 第二次翻转,将最大饼翻到最下面 + reverse(cakes.begin(), cakes.begin() + n); + res.push_back(n); + + // 递归调用 + sort(cakes, n - 1); + } +}; +``` + +### java + +[L-WEIWEI](https://github.com/L-WWEEII) 提供 第969题的 Java 代码: + +```java +class Solution { + public List pancakeSort(int[] A) { + List ans = new ArrayList(); + int len = A.length; + if(len == 0){ + return ans; + } + // maxIndex[0] == 当前轮次的最大元素, maxIndex[1] == 最大元素下标 + int[] maxIndex = new int[2]; + maxIndex[0] = Integer.MIN_VALUE; + int maxCount = 0; + // maxCount == len 时,说明完成了整个数组的最大值沉底操作, + while(maxCount < len - 1){ + maxCount = maxValueDown(A, maxIndex, maxCount, ans); + // 每做完一次最大值沉底操作,初始化最大元素值 + maxIndex[0] = Integer.MIN_VALUE; + } + return ans; + } + public int maxValueDown(int[] A, int[] maxIndex, int maxCount, List ans){ + // 遍历条件为 i < A.length - maxCount , 每次最大值沉底时,maxCount + 1,因此下次遍历即可不对最后 maxCount 个元素做操作 + for(int i = 0; i < A.length - maxCount; i++){ + // 元素大于当前储存的元素时,将值与下标 copy 到 maxIndex 数组中 + if(A[i] > maxIndex[0]){ + maxIndex[0] = A[i]; + maxIndex[1] = i; + } + } + // 如果当前轮次最大元素的下标的下一位是上一轮次的最大下标,则不做翻转操作,直接返回 maxCount + 1 + if(maxIndex[1] + 1 == A.length - maxCount){ + return maxCount + 1; + } + // 使用最大值沉底时,当本轮最大值在首位时,不需要再将其先翻转至首位,所以不添加 + if(maxIndex[1] > 0){ + // 将该轮次要翻转的下标添加到结果集中,结果集中需要的是翻转的位置而不是下标,所以添加时下标得 + 1 + ans.add(maxIndex[1] + 1); + } + // 双指针原地交换数组中的值 + // 左指针指0 + int left = 0; + // 右指针指向当前轮次最大元素的下标 + int right = maxIndex[1]; + while(left < right){ + // 交换元素值 + A[left] += A[right]; + A[right] = A[left] - A[right]; + A[left] -= A[right]; + left++; + right--; + } + // 上面交换玩元素值后,当前轮次最大元素排在首位,再从上一轮次最大元素 - 1 的位置翻转 + // 则当前轮次的最大元素成功沉底 + ans.add(A.length - maxCount); + left = 0; + right = A.length - 1 - maxCount; + while(left < right){ + A[left] += A[right]; + A[right] = A[left] - A[right]; + A[left] -= A[right]; + left++; + right--; + } + return maxCount + 1; + } +} +``` diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\347\256\227\346\263\225\345\255\246\344\271\240\344\271\213\350\267\257.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\347\256\227\346\263\225\345\255\246\344\271\240\344\271\213\350\267\257.md" index 0446ffa4ec30d53215c7c9313ca875d85109e5d9..6f16d56aab08a2bc112a80325c8a53aab858978e 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\347\256\227\346\263\225\345\255\246\344\271\240\344\271\213\350\267\257.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\347\256\227\346\263\225\345\255\246\344\271\240\344\271\213\350\267\257.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [Git/SQL/正则表达式的在线练习平台](https://labuladong.gitbook.io/algo) - * [回溯算法团灭子集、排列、组合问题](https://labuladong.gitbook.io/algo) + * [Git/SQL/正则表达式的在线练习平台](https://labuladong.gitbook.io/algo/) + * [回溯算法团灭子集、排列、组合问题](https://labuladong.gitbook.io/algo/) **-----------** @@ -96,7 +96,7 @@ PS:**如果有的英文题目实在看不懂,有个小技巧**,你在题 **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\351\200\222\345\275\222\350\257\246\350\247\243.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\351\200\222\345\275\222\350\257\246\350\247\243.md" index b9a9928661a331d97a6be47afe7e34dd8a694f13..cc68a61e779970179a87c4277aacc86c89d1e9e6 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\351\200\222\345\275\222\350\257\246\350\247\243.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\351\200\222\345\275\222\350\257\246\350\247\243.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [特殊数据结构:单调队列](https://labuladong.gitbook.io/algo) - * [设计Twitter](https://labuladong.gitbook.io/algo) + * [特殊数据结构:单调队列](https://labuladong.gitbook.io/algo/) + * [设计Twitter](https://labuladong.gitbook.io/algo/) **-----------** @@ -267,7 +267,7 @@ https://leetcode.com/tag/divide-and-conquer/ **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/LRU\347\256\227\346\263\225.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/LRU\347\256\227\346\263\225.md" index 04ebea80fe0e176b1011c18c37d94391253964a1..5215f188a277dbf2bc01f93ae9cf5b495bd5eca2 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/LRU\347\256\227\346\263\225.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/LRU\347\256\227\346\263\225.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [25 张图解:键入网址后,到网页显示,其间发生了什么](https://labuladong.gitbook.io/algo) - * [如何在无限序列中随机抽取元素](https://labuladong.gitbook.io/algo) + * [25 张图解:键入网址后,到网页显示,其间发生了什么](https://labuladong.gitbook.io/algo/) + * [如何在无限序列中随机抽取元素](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -338,7 +338,7 @@ class LRUCache { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -347,6 +347,93 @@ class LRUCache {

======其他语言代码====== + +[gowufang](https://github.com/gowufang)提供第146题C++代码: +```cpp +class LRUCache { + public: + struct node { + int val; + int key; + node* pre;//当前节点的前一个节点 + node* next;//当前节点的后一个节点 + node(){} + node(int key, int val):key(key), val(val), pre(NULL), next(NULL){} + }; + + LRUCache(int size) { + this->size = size; + head = new node(); + tail = new node(); + head->next = tail; + tail->pre = head; + } + + + void movetohead(node* cur)//相当于一个insert操作,在head 和 head的next之间插入一个节点 + { + node* next = head->next;//head的next先保存起来 + head->next = cur;//将当前节点移动到head的后面 + cur->pre = head;//当前节点cur的pre指向head + next->pre = cur; + cur->next = next; + } + + node* deletecurrentnode(node* cur)//移除当前节点 + { + cur->pre->next = cur->next; + cur->next->pre = cur->pre; + return cur; + } + void makerecently(node* cur) + { + node* temp = deletecurrentnode(cur);// 删除 cur,要重新插入到对头 + movetohead(temp);//cur放到队头去 + } + int get(int key) + { + int ret = -1; + if ( map.count(key)) + { + node* temp = map[key]; + makerecently(temp);// 将 key 变为最近使用 + ret = temp->val; + } + return ret; + } + + void put(int key, int value) { + if ( map.count(key)) + { + // 修改 key 的值 + node* temp = map[key]; + temp->val = value; + // 将 key 变为最近使用 + makerecently(temp); + } + else + { + node* cur = new node(key, value); + if( map.size()== size ) + { + // 链表头部就是最久未使用的 key + node *temp = deletecurrentnode(tail->pre); + map.erase(temp->key); + } + movetohead(cur); + map[key] = cur; + + } + + } + + unordered_map map; + int size; + node* head, *tail; + + }; +``` + ```python3 """ 所谓LRU缓存,根本的难点在于记录最久被使用的键值对,这就设计到排序的问题, diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/koko\345\201\267\351\246\231\350\225\211.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/koko\345\201\267\351\246\231\350\225\211.md" index cc880dd268762c333e98b2fc7fcbfb7374f9b234..2322bba49f75ed8456c32c3325518f6dbb80620b 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/koko\345\201\267\351\246\231\350\225\211.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/koko\345\201\267\351\246\231\350\225\211.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [如何运用贪心思想玩跳跃游戏](https://labuladong.gitbook.io/algo) - * [如何寻找最长回文子串](https://labuladong.gitbook.io/algo) + * [如何运用贪心思想玩跳跃游戏](https://labuladong.gitbook.io/algo/) + * [如何寻找最长回文子串](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -161,7 +161,7 @@ for (int i = 0; i < n; i++) **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -169,4 +169,81 @@ for (int i = 0; i < n; i++)

-======其他语言代码====== \ No newline at end of file +======其他语言代码====== + +#### c++ +[cchroot](https://github.com/cchroot) 提供 C++ 代码: + +```c++ +class Solution { +public: + int minEatingSpeed(vector& piles, int H) { + // 二分法查找最小速度 + // 初始化最小速度为 1,最大速度为题目设定的最大值 10^9 + // 这里也可以遍历 piles 数组,获取数组中的最大值,设置 right 为数组中的最大值即可(因为每堆香蕉1小时吃完是最快的) + // log2(10^9) 约等于30,次数不多,所以这里暂时就不采取遍历获取最大值了 + int left = 1, right = pow(10, 9); + while (left < right) { // 二分法基本的防止溢出 + int mid = left + (right - left) / 2; + // 以 mid 的速度吃香蕉,是否能在 H 小时内吃完香蕉 + if (!canFinish(piles, mid, H)) + left = mid + 1; + else + right = mid; + } + return left; + } + + // 以 speed 的速度是否能把香蕉吃完 + bool canFinish(vector& piles, int speed, int H) { + int time = 0; + // 遍历累加时间 time + for (int p: piles) + time += (p - 1) / speed + 1; + return time <= H; // time 小于等于 H 说明能在 H 小时吃完返回 true, 否则返回 false + } +}; +``` + +### python +[tonytang731](https://https://github.com/tonytang731) 提供 Python3 代码: + +```python +import math + +class Solution: + def minEatingSpeed(self, piles, H): + # 初始化起点和终点, 最快的速度可以一次拿完最大的一堆 + start = 1 + end = max(piles) + + # while loop进行二分查找 + while start + 1 < end: + mid = start + (end - start) // 2 + + # 如果中点所需时间大于H, 我们需要加速, 将起点设为中点 + if self.timeH(piles, mid) > H: + start = mid + # 如果中点所需时间小于H, 我们需要减速, 将终点设为中点 + else: + end = mid + + # 提交前确认起点是否满足条件,我们要尽量慢拿 + if self.timeH(piles, start) <= H: + return start + + # 若起点不符合, 则中点是答案 + return end + + + + def timeH(self, piles, K): + # 初始化时间 + H = 0 + + #求拿每一堆需要多长时间 + for pile in piles: + H += math.ceil(pile / K) + + return H +``` diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/k\344\270\252\344\270\200\347\273\204\345\217\215\350\275\254\351\223\276\350\241\250.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/k\344\270\252\344\270\200\347\273\204\345\217\215\350\275\254\351\223\276\350\241\250.md" index 83fbd986c8c6d88349dc3bfd93ea28de28c777bf..1766b4a159b118e642db0793bf743338ba3f269b 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/k\344\270\252\344\270\200\347\273\204\345\217\215\350\275\254\351\223\276\350\241\250.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/k\344\270\252\344\270\200\347\273\204\345\217\215\350\275\254\351\223\276\350\241\250.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [区间调度之区间交集问题](https://labuladong.gitbook.io/algo) - * [动态规划和回溯算法到底谁是谁爹?](https://labuladong.gitbook.io/algo) + * [区间调度之区间交集问题](https://labuladong.gitbook.io/algo/) + * [动态规划和回溯算法到底谁是谁爹?](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -32,7 +32,7 @@ ### 一、分析问题 -首先,前文[学习数据结构的框架思维](https://labuladong.gitbook.io/algo)提到过,链表是一种兼具递归和迭代性质的数据结构,认真思考一下可以发现**这个问题具有递归性质**。 +首先,前文[学习数据结构的框架思维](https://labuladong.gitbook.io/algo/)提到过,链表是一种兼具递归和迭代性质的数据结构,认真思考一下可以发现**这个问题具有递归性质**。 什么叫递归性质?直接上图理解,比如说我们对这个链表调用 `reverseKGroup(head, 2)`,即以 2 个节点为一组反转链表: @@ -148,7 +148,7 @@ ListNode reverseKGroup(ListNode head, int k) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\344\270\200\350\241\214\344\273\243\347\240\201\350\247\243\345\206\263\347\232\204\346\231\272\345\212\233\351\242\230.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\344\270\200\350\241\214\344\273\243\347\240\201\350\247\243\345\206\263\347\232\204\346\231\272\345\212\233\351\242\230.md" index 1d177823c168344d8d20fce523a76bf7b228ba23..af43a75da58eee6a91c3b1c4a75d4a3c66fd8608 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\344\270\200\350\241\214\344\273\243\347\240\201\350\247\243\345\206\263\347\232\204\346\231\272\345\212\233\351\242\230.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\344\270\200\350\241\214\344\273\243\347\240\201\350\247\243\345\206\263\347\232\204\346\231\272\345\212\233\351\242\230.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [学习算法和数据结构的思路指南](https://labuladong.gitbook.io/algo) - * [我用四个命令概括了 Git 的所有套路](https://labuladong.gitbook.io/algo) + * [学习算法和数据结构的思路指南](https://labuladong.gitbook.io/algo/) + * [我用四个命令概括了 Git 的所有套路](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -141,7 +141,7 @@ int bulbSwitch(int n) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -149,4 +149,58 @@ int bulbSwitch(int n) {

-======其他语言代码====== \ 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& piles) { + // 双方都很聪明的前提下, 先手必胜无疑 + return true; + } +}; +``` + + +由[JodyZ0203](https://github.com/JodyZ0203)提供 319. 灯泡开关 Python3 解法代码: + +```Python +class Solution: + def bulbSwitch(self, n: int) -> int: + # 平方根电灯个数之后向下取整即可 + return floor(sqrt (n)) +``` + +由[JodyZ0203](https://github.com/JodyZ0203)提供 319. 灯泡开关 C++ 解法代码: + +```cpp +class Solution { +public: + int bulbSwitch(int n) { + // 平方根电灯个数之后向下取整即可 + return floor(sqrt (n)); + } +}; +``` diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\344\272\214\345\210\206\346\237\245\346\211\276\345\210\244\345\256\232\345\255\220\345\272\217\345\210\227.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\344\272\214\345\210\206\346\237\245\346\211\276\345\210\244\345\256\232\345\255\220\345\272\217\345\210\227.md" index c71e65f05a754dd808261eadb162b221d5939215..f5228915d0694515d4c7903a42ebe356c7567775 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\344\272\214\345\210\206\346\237\245\346\211\276\345\210\244\345\256\232\345\255\220\345\272\217\345\210\227.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\344\272\214\345\210\206\346\237\245\346\211\276\345\210\244\345\256\232\345\255\220\345\272\217\345\210\227.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [twoSum问题的核心思想](https://labuladong.gitbook.io/algo) - * [经典动态规划:子集背包问题](https://labuladong.gitbook.io/algo) + * [twoSum问题的核心思想](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/) 就借助一个纸牌游戏衍生出二分查找解法。 今天再讲一道巧用二分查找的算法问题:如何判定字符串 `s` 是否是字符串 `t` 的子序列(可以假定 `s` 长度比较小,且 `t` 的长度非常大)。举两个例子: @@ -95,7 +95,7 @@ for (int i = 0; i < n; i++) { ### 三、再谈二分查找 -在前文 [二分查找详解](https://labuladong.gitbook.io/algo) 中,详解了如何正确写出三种二分查找算法的细节。二分查找返回目标值 `val` 的索引,对于搜索**左侧边界**的二分查找,有一个特殊性质: +在前文 [二分查找详解](https://labuladong.gitbook.io/algo/) 中,详解了如何正确写出三种二分查找算法的细节。二分查找返回目标值 `val` 的索引,对于搜索**左侧边界**的二分查找,有一个特殊性质: **当 `val` 不存在时,得到的索引恰好是比 `val` 大的最小元素索引**。 @@ -160,7 +160,7 @@ boolean isSubsequence(String s, String t) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -168,4 +168,67 @@ boolean isSubsequence(String s, String t) {

-======其他语言代码====== \ 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 index[256]; + for (int i = 0; i < n; i++) { + char c = t[i]; + index[c].push_back(i); + } + // 串 t 上的指针 + int j = 0; + // 借助 index 查找 s[i] + for (int i = 0; i < m; i++) { + char c = s[i]; + // 整个 t 压根儿没有字符 c + if (index[c].empty()) return false; + int pos = left_bound(index[c], j); + // 二分搜索区间中没有找到字符 c + if (pos == index[c].size()) return false; + // 向前移动指针 j + j = index[c][pos] + 1; + } + return true; + } + // 查找左侧边界的二分查找 + int left_bound(vector arr, int tar) { + int lo = 0, hi = arr.size(); + while (lo < hi) { + int mid = lo + (hi - lo) / 2; + if (tar > arr[mid]) { + lo = mid + 1; + } else { + hi = mid; + } + } + return lo; + } +}; +``` diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\210\244\346\226\255\345\233\236\346\226\207\351\223\276\350\241\250.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\210\244\346\226\255\345\233\236\346\226\207\351\223\276\350\241\250.md" index 3e2406886f5d6709db893951d2585cf6f770f371..bb1a6aa05febf2ec7b095b4162ef784014116697 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\210\244\346\226\255\345\233\236\346\226\207\351\223\276\350\241\250.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\210\244\346\226\255\345\233\236\346\226\207\351\223\276\350\241\250.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [如何高效进行模幂运算](https://labuladong.gitbook.io/algo) - * [一文学会递归解题](https://labuladong.gitbook.io/algo) + * [如何高效进行模幂运算](https://labuladong.gitbook.io/algo/) + * [一文学会递归解题](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -229,7 +229,7 @@ p.next = reverse(q); **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -237,4 +237,35 @@ p.next = reverse(q);

-======其他语言代码====== \ 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/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\220\210\346\263\225\346\213\254\345\217\267\345\210\244\345\256\232.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\220\210\346\263\225\346\213\254\345\217\267\345\210\244\345\256\232.md" index 6516aa1b53ed0c47efd7abe27e8229e37bd23a5b..c183b510d67d9f69b4cb8f821a32dee0ceff95d0 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\220\210\346\263\225\346\213\254\345\217\267\345\210\244\345\256\232.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\220\210\346\263\225\346\213\254\345\217\267\345\210\244\345\256\232.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [回溯算法解题套路框架](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/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\246\202\344\275\225\345\216\273\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\246\202\344\275\225\345\216\273\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.md" index 1cdec24bdf335e12ac3e8c97a5be4daef997a65a..4e662a8aa0b61ca5108122a4bea61fc6a2bc16b4 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\246\202\344\275\225\345\216\273\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\246\202\344\275\225\345\216\273\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.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/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\255\220\351\233\206\346\216\222\345\210\227\347\273\204\345\220\210.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\255\220\351\233\206\346\216\222\345\210\227\347\273\204\345\220\210.md" index 04fe3bb3363ac9d659c3b39e1f88636a9ed24957..cda2e83e18635aec8954cee97ec32b10a59d2f66 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\255\220\351\233\206\346\216\222\345\210\227\347\273\204\345\220\210.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\255\220\351\233\206\346\216\222\345\210\227\347\273\204\345\220\210.md" @@ -12,8 +12,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [回溯算法解题套路框架](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 track) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。

+======其他语言代码====== -======其他语言代码====== \ No newline at end of file +[userLF](https://github.com/userLF)提供全排列的java代码: + +```java +import java.util.ArrayList; +import java.util.List; + +class Solution { + List> res = new ArrayList<>(); + public List> permute(int[] nums) { + res.clear(); + dfs(nums, 0);// + return res; + } + + public void dfs(int[] n, int start) {//start表示要被替换元素的位置 + if( start >= n.length) { + List list = new ArrayList(); + for(int i : n) { + list.add(i); + } + res.add(list); + return; + } + + for(int i = start; i< n.length; i++) {//i从start开始,如果从start+1开始的话,会把当前序列遗漏掉直接保存了下一个序列 + int temp= n[i]; + n[i] = n[start]; + n[start] = temp; + dfs(n, start + 1);//递归下一个位置 + //回到上一个状态 + n[start] = n[i]; + n[i] = temp; + } + } +} + +``` \ No newline at end of file diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\272\247\344\275\215\350\260\203\345\272\246.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\272\247\344\275\215\350\260\203\345\272\246.md" index 21e45efe136341be16d231df432bc156f6fbb9ed..4fbca5f8bb09a29e092bc33be850475a6b11fa78 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\272\247\344\275\215\350\260\203\345\272\246.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\345\272\247\344\275\215\350\260\203\345\272\246.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [一个方法团灭 LeetCode 股票买卖问题](https://labuladong.gitbook.io/algo) - * [Linux shell 的实用小技巧](https://labuladong.gitbook.io/algo) + * [一个方法团灭 LeetCode 股票买卖问题](https://labuladong.gitbook.io/algo/) + * [Linux shell 的实用小技巧](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -225,7 +225,7 @@ private int distance(int[] intv) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\211\223\345\215\260\347\264\240\346\225\260.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\211\223\345\215\260\347\264\240\346\225\260.md" index 3a580746077e238ebd17d17026aa7177bede719e..8e71f3e0e7119e05d64abe094a9537016088bf36 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\211\223\345\215\260\347\264\240\346\225\260.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\211\223\345\215\260\347\264\240\346\225\260.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [算法就像搭乐高:带你手撸 LRU 算法](https://labuladong.gitbook.io/algo) - * [区间调度之区间合并问题](https://labuladong.gitbook.io/algo) + * [算法就像搭乐高:带你手撸 LRU 算法](https://labuladong.gitbook.io/algo/) + * [区间调度之区间合并问题](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -170,7 +170,7 @@ int countPrimes(int n) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\216\245\351\233\250\346\260\264.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\216\245\351\233\250\346\260\264.md" index a500aac0f73498427cb2908cd78480037cc11b1c..87357fe25086f353c3753a88a2e82ae14dd31e24 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\216\245\351\233\250\346\260\264.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\216\245\351\233\250\346\260\264.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [手把手带你刷二叉树(第三期)](https://labuladong.gitbook.io/algo) - * [45张图解:IP基础知识全家桶](https://labuladong.gitbook.io/algo) + * [手把手带你刷二叉树(第三期)](https://labuladong.gitbook.io/algo/) + * [45张图解:IP基础知识全家桶](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -203,7 +203,7 @@ if (l_max < r_max) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" index 9f0f37f526068ecfda595768105f1e194afb258d..58732779d27c9b5448b2b40f6a287961f4bd8295 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -12,8 +12,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [如何运用二分查找算法](https://labuladong.gitbook.io/algo) - * [动态规划答疑篇](https://labuladong.gitbook.io/algo) + * [如何运用二分查找算法](https://labuladong.gitbook.io/algo/) + * [动态规划答疑篇](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -123,7 +123,7 @@ string longestPalindrome(string s) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -131,4 +131,40 @@ string longestPalindrome(string s) {

-======其他语言代码====== \ 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/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\260\264\345\241\230\346\212\275\346\240\267.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\260\264\345\241\230\346\212\275\346\240\267.md" index fe0dd09dc5934a109cad928d44542a2359e2f940..e107ef5d337a6b849cfe01ca6ef5a5e8865af9ba 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\260\264\345\241\230\346\212\275\346\240\267.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\260\264\345\241\230\346\212\275\346\240\267.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [一文看懂 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/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\266\210\345\244\261\347\232\204\345\205\203\347\264\240.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\266\210\345\244\261\347\232\204\345\205\203\347\264\240.md" index 844b5cb6ff0f178fe304042ad519e56cf261fdc0..e434b5032aa053e7a07cd6e1d609cc2b5059d2f7 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\266\210\345\244\261\347\232\204\345\205\203\347\264\240.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\266\210\345\244\261\347\232\204\345\205\203\347\264\240.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [学习算法和数据结构的思路指南](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**。 @@ -132,4 +133,49 @@ public int missingNumber(int[] nums) {

-======其他语言代码====== \ No newline at end of file +======其他语言代码====== + +[happy-yuxuan](https://github.com/happy-yuxuan) 提供 三种方法的 C++ 代码: + +```c++ +// 方法:异或元素和索引 +int missingNumber(vector& nums) { + int n = nums.size(); + int res = 0; + // 先和新补的索引异或一下 + res ^= n; + // 和其他的元素、索引做异或 + for (int i = 0; i < n; i++) + res ^= i ^ nums[i]; + return res; +} +``` + +```c++ +// 方法:等差数列求和 +int missingNumber(vector& nums) { + int n = nums.size(); + // 公式:(首项 + 末项) * 项数 / 2 + int expect = (0 + n) * (n + 1) / 2; + int sum = 0; + for (int x : nums) + sum += x; + return expect - sum; +} +``` + +```c++ +// 方法:防止整型溢出 +int missingNumber(vector& nums) { + int n = nums.size(); + int res = 0; + // 新补的索引 + res += n - 0; + // 剩下索引和元素的差加起来 + for (int i = 0; i < n; i++) + res += i - nums[i]; + return res; +} +``` + + diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\347\274\272\345\244\261\345\222\214\351\207\215\345\244\215\347\232\204\345\205\203\347\264\240.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\347\274\272\345\244\261\345\222\214\351\207\215\345\244\215\347\232\204\345\205\203\347\264\240.md" index 06e554f1c8476c128f14a26af855883d2abce129..defccd33c9be92b7f91fdfaa7413b9032364350d 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\347\274\272\345\244\261\345\222\214\351\207\215\345\244\215\347\232\204\345\205\203\347\264\240.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\347\274\272\345\244\261\345\222\214\351\207\215\345\244\215\347\232\204\345\205\203\347\264\240.md" @@ -11,8 +11,8 @@ ![](../pictures/souyisou.png) 相关推荐: - * [手把手带你刷二叉树(第三期)](https://labuladong.gitbook.io/algo) - * [25 张图解:键入网址后,到网页显示,其间发生了什么](https://labuladong.gitbook.io/algo) + * [手把手带你刷二叉树(第三期)](https://labuladong.gitbook.io/algo/) + * [25 张图解:键入网址后,到网页显示,其间发生了什么](https://labuladong.gitbook.io/algo/) 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: @@ -131,7 +131,7 @@ vector findErrorNums(vector& nums) { **_____________** -**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo/) 持续更新最新文章**。 **本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 @@ -139,4 +139,30 @@ vector findErrorNums(vector& nums) {

-======其他语言代码====== \ 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}; + } +} +```