diff --git a/README.md b/README.md index 5fe165a7..d3b4fe66 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,11 @@ > 1. **介绍** :本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者) > 2. **正式出版** :[《代码随想录》](https://programmercarl.com/other/publish.html) 。 > 3. **PDF版本** :[「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/other/algo_pdf.html) 。 -> 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.bilibili.com/video/BV1fA4y1o715) 。 +> 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.programmercarl.com/other/gongkaike.html) 。 > 5. **最强八股文** :[代码随想录知识星球精华PDF](https://www.programmercarl.com/other/kstar_baguwen.html) 。 > 6. **刷题顺序** :README已经将刷题顺序排好了,按照顺序一道一道刷就可以。 > 7. **学习社区** :一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html) 。 -> 8. **提交代码** :本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。 +> 8. **提交代码** :本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://www.programmercarl.com/qita/join.html)了解提交代码的方式。 > 9. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境! @@ -51,19 +51,12 @@ ## 如何使用该刷题攻略 -电脑端还看不到留言,大家可以在公众号[「代码随想录」](https://img-blog.csdnimg.cn/20201124161234338.png),左下角有「刷题攻略」,这是手机版刷题攻略,看完就会发现有很多录友(代码随想录的朋友们)在文章下留言打卡,这份刷题顺序和题解已经陪伴了上万录友了,同时也说明文章的质量是经过上万人的考验! - -欢迎每一位学习算法的小伙伴加入到这个学习阵营来! - -**目前已经更新了,数组-> 链表-> 哈希表->字符串->栈与队列->树->回溯->贪心,八个专题了,正在讲解动态规划!** +按照先面的排列顺序,从数组开始刷起就可以了,顺序都安排好了,按顺序刷就好。 在刷题攻略中,每个专题开始都有理论基础篇,并不像是教科书般的理论介绍,而是从实战中归纳需要的基础知识。每个专题结束都有总结篇,最这个专题的归纳总结。 如果你是算法老手,这篇攻略也是复习的最佳资料,如果把每个系列对应的总结篇,快速过一遍,整个算法知识体系以及各种解法就重现脑海了。 - -目前「代码随想录」刷题攻略更新了:**200多篇文章,精讲了200道经典算法题目,共60w字的详细图解,大部分题目都搭配了20分钟左右的视频讲解**,视频质量很好,口碑很好,大家可以去看看,视频列表:[代码随想录视频讲解](https://www.bilibili.com/video/BV1fA4y1o715)。 - **这里每一篇题解,都是精品,值得仔细琢磨**。 我在题目讲解中统一使用C++,但你会发现下面几乎每篇题解都配有其他语言版本,Java、Python、Go、JavaScript等等,正是这些[热心小伙们](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)贡献的代码,当然我也会严格把控代码质量。 @@ -100,14 +93,11 @@ * [程序员应该用什么用具来写文档?](./problems/前序/程序员写文档工具.md) * 求职 + * [ACM模式练习网站,卡码网](https://kamacoder.com/) * [程序员的简历应该这么写!!(附简历模板)](./problems/前序/程序员简历.md) + * [【专业技能】应该这样写!](https://programmercarl.com/other/jianlizhuanye.html) + * [【项目经历】应该这样写!](https://programmercarl.com/other/jianlixiangmu.html) * [BAT级别技术面试流程和注意事项都在这里了](./problems/前序/BAT级别技术面试流程和注意事项都在这里了.md) - * [北京有这些互联网公司,你都知道么?](./problems/前序/北京互联网公司总结.md) - * [上海有这些互联网公司,你都知道么?](./problems/前序/上海互联网公司总结.md) - * [深圳有这些互联网公司,你都知道么?](./problems/前序/深圳互联网公司总结.md) - * [广州有这些互联网公司,你都知道么?](./problems/前序/广州互联网公司总结.md) - * [成都有这些互联网公司,你都知道么?](./problems/前序/成都互联网公司总结.md) - * [杭州有这些互联网公司,你都知道么?](./problems/前序/杭州互联网公司总结.md) * 算法性能分析 * [关于时间复杂度,你不知道的都在这里!](./problems/前序/关于时间复杂度,你不知道的都在这里!.md) @@ -506,7 +496,7 @@ # 关于作者 -大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发,CSDN博客专家。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。 +大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。 加入「代码随想录」刷题小分队(微信群),可以扫下方二维码,加代码随想录客服微信。 @@ -527,4 +517,3 @@
- diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index e3fb0fb5..712bc3f0 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -163,7 +163,7 @@ class Solution: for index, value in enumerate(nums): if target - value in records: # 遍历当前元素,并在map中寻找是否有匹配的key return [records[target- value], index] - records[value] = index # 遍历当前元素,并在map中寻找是否有匹配的key + records[value] = index # 如果没找到匹配对,就把访问过的元素和下标加入到map中 return [] ``` (版本二)使用集合 @@ -318,7 +318,7 @@ function twoSum(nums: number[], target: number): number[] { }; ``` -### php: +### PhP: ```php function twoSum(array $nums, int $target): array @@ -501,3 +501,4 @@ int* twoSum(int* nums, int numsSize, int target, int* returnSize){ + diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index b1987b87..614f6051 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -30,17 +30,17 @@ * 输出:"a" -# 思路 +## 思路 本题和[647.回文子串](https://programmercarl.com/0647.回文子串.html) 差不多是一样的,但647.回文子串更基本一点,建议可以先做647.回文子串 -## 暴力解法 +### 暴力解法 两层for循环,遍历区间起始位置和终止位置,然后判断这个区间是不是回文。 时间复杂度:O(n^3) -## 动态规划 +### 动态规划 动规五部曲: @@ -208,7 +208,7 @@ public: * 时间复杂度:O(n^2) * 空间复杂度:O(n^2) -## 双指针 +### 双指针 动态规划的空间复杂度是偏高的,我们再看一下双指针法。 @@ -258,9 +258,9 @@ public: -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java // 双指针 动态规划 @@ -327,7 +327,7 @@ class Solution { } ``` -Python: +### Python: ```python class Solution: @@ -377,7 +377,7 @@ class Solution: return s[start:end] ``` -Go: +### Go: ```go func longestPalindrome(s string) string { @@ -411,7 +411,7 @@ func longestPalindrome(s string) string { ``` -JavaScript: +### JavaScript: ```js //动态规划解法 @@ -527,7 +527,7 @@ var longestPalindrome = function(s) { }; ``` -C: +### C: 动态规划: ```c @@ -615,7 +615,7 @@ char * longestPalindrome(char * s){ } ``` -C#: +### C#: 動態規則: ```c# @@ -681,3 +681,4 @@ public class Solution { + diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index ce9eccf0..40ee3a2e 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -152,10 +152,10 @@ public: ## 相关题目推荐 -* 26.删除排序数组中的重复项 -* 283.移动零 -* 844.比较含退格的字符串 -* 977.有序数组的平方 +* [26.删除排序数组中的重复项](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/) +* [283.移动零](https://leetcode.cn/problems/move-zeroes/) +* [844.比较含退格的字符串](https://leetcode.cn/problems/backspace-string-compare/) +* [977.有序数组的平方](https://leetcode.cn/problems/squares-of-a-sorted-array/) ## 其他语言版本 @@ -444,3 +444,4 @@ public class Solution { + diff --git a/problems/0031.下一个排列.md b/problems/0031.下一个排列.md index 34aa1086..3cfb673a 100644 --- a/problems/0031.下一个排列.md +++ b/problems/0031.下一个排列.md @@ -34,7 +34,7 @@ * 输出:[1] -# 思路 +## 思路 一些同学可能手动写排列的顺序,都没有写对,那么写程序的话思路一定是有问题的了,我这里以1234为例子,把全排列都列出来。可以参考一下规律所在: @@ -92,9 +92,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -159,7 +159,7 @@ class Solution { } ``` -## Python +### Python >直接使用sorted()会开辟新的空间并返回一个新的list,故补充一个原地反转函数 ```python class Solution: @@ -191,7 +191,7 @@ class Solution: """ ``` -## Go +### Go ```go //卡尔的解法 @@ -216,7 +216,7 @@ func reverse(a []int,begin,end int){ } ``` -## JavaScript +### JavaScript ```js //卡尔的解法(吐槽一下JavaScript的sort和其他语言的不太一样,只想到了拷贝数组去排序再替换原数组来实现nums的[i + 1, nums.length)升序排序) @@ -272,3 +272,4 @@ var nextPermutation = function(nums) { + diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md index db66095d..1f1a543b 100644 --- a/problems/0042.接雨水.md +++ b/problems/0042.接雨水.md @@ -29,7 +29,7 @@ * 输出:9 -# 思路 +## 思路 接雨水问题在面试中还是常见题目的,有必要好好讲一讲。 @@ -39,7 +39,7 @@ * 动态规划 * 单调栈 -## 暴力解法 +### 暴力解法 本题暴力解法也是也是使用双指针。 @@ -137,7 +137,7 @@ public: 力扣后面修改了后台测试数据,所以以上暴力解法超时了。 -## 双指针优化 +### 双指针优化 在暴力解法中,我们可以看到只要记录左边柱子的最高高度 和 右边柱子的最高高度,就可以计算当前位置的雨水面积,这就是通过列来计算。 @@ -184,7 +184,7 @@ public: }; ``` -## 单调栈解法 +### 单调栈解法 关于单调栈的理论基础,单调栈适合解决什么问题,单调栈的工作过程,大家可以先看这题讲解 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 @@ -194,7 +194,7 @@ public: 而接雨水这道题目,我们正需要寻找一个元素,右边最大元素以及左边最大元素,来计算雨水面积。 -### 准备工作 +#### 准备工作 那么本题使用单调栈有如下几个问题: @@ -248,7 +248,7 @@ stack st; // 存着下标,计算的时候用下标对应的柱子高度 明确了如上几点,我们再来看处理逻辑。 -### 单调栈处理逻辑 +#### 单调栈处理逻辑 以下操作过程其实和 [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 也是一样的,建议先做 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 @@ -596,7 +596,7 @@ class Solution: ``` -### Go +### Go: ```go func trap(height []int) int { @@ -802,7 +802,7 @@ var trap = function(height) { }; ``` -### TypeScript +### TypeScript: 暴力解法: @@ -925,8 +925,108 @@ int trap(int* height, int heightSize) { * 时间复杂度 O(n) * 空间复杂度 O(1) +### Rust: + +双指针 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let n = height.len(); + let mut max_left = vec![0; height.len()]; + let mut max_right = vec![0; height.len()]; + max_left.iter_mut().zip(max_right.iter_mut().rev()).enumerate().fold((0, 0), |(lm, rm), (idx, (x, y))| { + let lmax = lm.max(height[idx]); + let rmax = rm.max(height[n - 1 - idx]); + *x = lmax; *y = rmax; + (lmax, rmax) + }); + height.iter().enumerate().fold(0, |acc, (idx, x)| { + let h = max_left[idx].min(max_right[idx]); + if h > 0 { h - x + acc } else { acc } + }) + } +} +``` + +单调栈 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let mut stack = vec![]; + let mut ans = 0; + for (right_pos, &right_h) in height.iter().enumerate() { + while !stack.is_empty() && height[*stack.last().unwrap()] <= right_h { + let mid_pos = stack.pop().unwrap(); + if !stack.is_empty() { + let left_pos = *stack.last().unwrap(); + let left_h = height[left_pos]; + let top = std::cmp::min(left_h, right_h); + if top > height[mid_pos] { + ans += (top - height[mid_pos]) * (right_pos - left_pos - 1) as i32; + } + } + } + stack.push(right_pos); + } + ans + } +} +``` + +Rust + +双指针 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let n = height.len(); + let mut max_left = vec![0; height.len()]; + let mut max_right = vec![0; height.len()]; + max_left.iter_mut().zip(max_right.iter_mut().rev()).enumerate().fold((0, 0), |(lm, rm), (idx, (x, y))| { + let lmax = lm.max(height[idx]); + let rmax = rm.max(height[n - 1 - idx]); + *x = lmax; *y = rmax; + (lmax, rmax) + }); + height.iter().enumerate().fold(0, |acc, (idx, x)| { + let h = max_left[idx].min(max_right[idx]); + if h > 0 { h - x + acc } else { acc } + }) + } +} +``` + +单调栈 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let mut stack = vec![]; + let mut ans = 0; + for (right_pos, &right_h) in height.iter().enumerate() { + while !stack.is_empty() && height[*stack.last().unwrap()] <= right_h { + let mid_pos = stack.pop().unwrap(); + if !stack.is_empty() { + let left_pos = *stack.last().unwrap(); + let left_h = height[left_pos]; + let top = std::cmp::min(left_h, right_h); + if top > height[mid_pos] { + ans += (top - height[mid_pos]) * (right_pos - left_pos - 1) as i32; + } + } + } + stack.push(right_pos); + } + ans + } +} +```

+ diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index 2f0349b2..02c8e486 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -25,9 +25,9 @@ 说明: 假设你总是可以到达数组的最后一个位置。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,最少跳几步还得看覆盖范围 | LeetCode: 45.跳跃游戏 II](https://www.bilibili.com/video/BV1Y24y1r7XZ),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,最少跳几步还得看覆盖范围 | LeetCode: 45.跳跃游戏 II](https://www.bilibili.com/video/BV1Y24y1r7XZ),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -53,7 +53,7 @@ **图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)** -## 方法一 +### 方法一 从图中可以看出来,就是移动下标达到了当前覆盖的最远距离下标时,步数就要加一,来增加覆盖距离。最后的步数就是最少步数。 @@ -90,7 +90,7 @@ public: * 空间复杂度: O(1) -## 方法二 +### 方法二 依然是贪心,思路和方法一差不多,代码可以简洁一些。 @@ -469,3 +469,4 @@ impl Solution { + diff --git a/problems/0052.N皇后II.md b/problems/0052.N皇后II.md index ac774c83..29c2b588 100644 --- a/problems/0052.N皇后II.md +++ b/problems/0052.N皇后II.md @@ -310,3 +310,4 @@ class Solution { + diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index fe4e4ed3..639c54bc 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -16,11 +16,13 @@ - 输出: 6 - 解释:  连续子数组  [4,-1,2,1] 的和最大,为  6。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法的巧妙需要慢慢体会!LeetCode:53. 最大子序和](https://www.bilibili.com/video/BV1aY4y1Z7ya),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法的巧妙需要慢慢体会!LeetCode:53. 最大子序和](https://www.bilibili.com/video/BV1aY4y1Z7ya),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -## 暴力解法 +## 思路 + +### 暴力解法 暴力解法的思路,第一层 for 就是设置起始位置,第二层 for 循环遍历数组寻找最大值 @@ -48,7 +50,7 @@ public: 以上暴力的解法 C++勉强可以过,其他语言就不确定了。 -## 贪心解法 +### 贪心解法 **贪心贪的是哪里呢?** @@ -104,7 +106,7 @@ public: 当然题目没有说如果数组为空,应该返回什么,所以数组为空的话返回啥都可以了。 -## 常见误区 +### 常见误区 误区一: @@ -122,7 +124,7 @@ public: 其实并不会,因为还有一个变量 result 一直在更新 最大的连续和,只要有更大的连续和出现,result 就更新了,那么 result 已经把 4 更新了,后面 连续和变成 3,也不会对最后结果有影响。 -## 动态规划 +### 动态规划 当然本题还可以用动态规划来做,在代码随想录动态规划章节我会详细介绍,如果大家想在想看,可以直接跳转:[动态规划版本详解](https://programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html#%E6%80%9D%E8%B7%AF) diff --git a/problems/0053.最大子序和(动态规划).md b/problems/0053.最大子序和(动态规划).md index 6f3b3686..f1b64709 100644 --- a/problems/0053.最大子序和(动态规划).md +++ b/problems/0053.最大子序和(动态规划).md @@ -17,7 +17,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[看起来复杂,其实是简单动态规划 | LeetCode:53.最大子序和](https://www.bilibili.com/video/BV19V4y1F7b5),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[看起来复杂,其实是简单动态规划 | LeetCode:53.最大子序和](https://www.bilibili.com/video/BV19V4y1F7b5),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -97,8 +97,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java /** * 1.dp[i]代表当前下标对应的最大值 @@ -140,7 +140,8 @@ class Solution { } ``` -Python: +### Python: + ```python class Solution: def maxSubArray(self, nums: List[int]) -> int: @@ -153,7 +154,8 @@ class Solution: return result ``` -Go: +### Go: + ```Go // solution // 1, dp @@ -184,7 +186,7 @@ func max(a,b int) int{ } ``` -JavaScript: +### JavaScript: ```javascript const maxSubArray = nums => { @@ -203,8 +205,7 @@ const maxSubArray = nums => { }; ``` - -Scala: +### Scala: ```scala object Solution { @@ -221,7 +222,7 @@ object Solution { } ``` -TypeScript: +### TypeScript: ```typescript function maxSubArray(nums: number[]): number { @@ -244,3 +245,4 @@ function maxSubArray(nums: number[]): number { + diff --git a/problems/0054.螺旋矩阵.md b/problems/0054.螺旋矩阵.md index a38e8237..d855f1a1 100644 --- a/problems/0054.螺旋矩阵.md +++ b/problems/0054.螺旋矩阵.md @@ -6,7 +6,7 @@ -## 54.螺旋矩阵 +# 54.螺旋矩阵 [力扣题目链接](https://leetcode.cn/problems/spiral-matrix/) diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md index e54c2034..bedb09ab 100644 --- a/problems/0055.跳跃游戏.md +++ b/problems/0055.跳跃游戏.md @@ -26,9 +26,9 @@ - 输出: false - 解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,怎么跳跃不重要,关键在覆盖范围 | LeetCode:55.跳跃游戏](https://www.bilibili.com/video/BV1VG4y1X7kB),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,怎么跳跃不重要,关键在覆盖范围 | LeetCode:55.跳跃游戏](https://www.bilibili.com/video/BV1VG4y1X7kB),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index 8705f840..95781b1a 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -22,9 +22,9 @@ * 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。 * 注意:输入类型已于2019年4月15日更改。 请重置默认代码定义以获取新方法签名。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,合并区间有细节!LeetCode:56.合并区间](https://www.bilibili.com/video/BV1wx4y157nD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,合并区间有细节!LeetCode:56.合并区间](https://www.bilibili.com/video/BV1wx4y157nD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -341,3 +341,4 @@ impl Solution { + diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index f03fcdad..78d9385a 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -688,6 +688,58 @@ public class Solution { } ``` +### Ruby: +```ruby +def generate_matrix(n) + result = Array.new(n) { Array.new(n, 0) } + #循环次数 + loop_times = 0 + #步长 + step = n - 1 + val = 1 + + + while loop_times < n / 2 + #模拟从左向右 + for i in 0..step - 1 + #行数不变,列数变 + result[loop_times][i+loop_times] = val + val += 1 + end + + #模拟从上到下 + for i in 0..step - 1 + #列数不变,行数变 + result[i+loop_times][n-loop_times-1] = val + val += 1 + end + + #模拟从右到左 + for i in 0..step - 1 + #行数不变,列数变 + result[n-loop_times-1][n-loop_times-i-1] = val + val += 1 + end + + #模拟从下到上 + for i in 0..step - 1 + #列数不变,行数变 + result[n-loop_times-i-1][loop_times] = val + val += 1 + end + + loop_times += 1 + step -= 2 + end + + #如果是奇数,则填充最后一个元素 + result[n/2][n/2] = n**2 if n % 2 + + return result + +end +``` +

diff --git a/problems/0062.不同路径.md b/problems/0062.不同路径.md index 5111e30e..985c7575 100644 --- a/problems/0062.不同路径.md +++ b/problems/0062.不同路径.md @@ -50,9 +50,9 @@ * 1 <= m, n <= 100 * 题目数据保证答案小于等于 2 * 10^9 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划中如何初始化很重要!| LeetCode:62.不同路径](https://www.bilibili.com/video/BV1ve4y1x7Eu/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划中如何初始化很重要!| LeetCode:62.不同路径](https://www.bilibili.com/video/BV1ve4y1x7Eu/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index cb305b41..3d243a7a 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -46,9 +46,9 @@ * 1 <= m, n <= 100 * obstacleGrid[i][j] 为 0 或 1 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,这次遇到障碍了| LeetCode:63. 不同路径 II](https://www.bilibili.com/video/BV1Ld4y1k7c6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,这次遇到障碍了| LeetCode:63. 不同路径 II](https://www.bilibili.com/video/BV1Ld4y1k7c6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md index 1b24e491..1a1f7e31 100644 --- a/problems/0070.爬楼梯.md +++ b/problems/0070.爬楼梯.md @@ -29,9 +29,9 @@ * 1 阶 + 2 阶 * 2 阶 + 1 阶 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[带你学透动态规划-爬楼梯|LeetCode:70.爬楼梯)](https://www.bilibili.com/video/BV17h411h7UH),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透动态规划-爬楼梯|LeetCode:70.爬楼梯)](https://www.bilibili.com/video/BV17h411h7UH),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -522,3 +522,4 @@ impl Solution { + diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index 8c85985f..4ca7a371 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -127,8 +127,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public int climbStairs(int n) { @@ -148,7 +148,7 @@ class Solution { } ``` -Python3: +### Python3: ```python @@ -166,8 +166,8 @@ class Solution: return dp[n] ``` +### Go: -Go: ```go func climbStairs(n int) int { //定义 @@ -189,7 +189,8 @@ func climbStairs(n int) int { } ``` -JavaScript: +### JavaScript: + ```javascript var climbStairs = function(n) { const dp = new Array(n + 1).fill(0); @@ -206,7 +207,7 @@ var climbStairs = function(n) { }; ``` -TypeScript: +### TypeScript: ```typescript function climbStairs(n: number): number { @@ -226,7 +227,7 @@ function climbStairs(n: number): number { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -250,4 +251,3 @@ impl Solution { - diff --git a/problems/0072.编辑距离.md b/problems/0072.编辑距离.md index 703e8913..1ed9a860 100644 --- a/problems/0072.编辑距离.md +++ b/problems/0072.编辑距离.md @@ -40,8 +40,8 @@ exection -> execution (插入 'u') * 0 <= word1.length, word2.length <= 500 * word1 和 word2 由小写英文字母组成 -# 算法公开课 -**《代码随想录》算法视频公开课:[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +## 算法公开课 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -227,8 +227,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java public int minDistance(String word1, String word2) { int m = word1.length(); @@ -256,7 +256,8 @@ public int minDistance(String word1, String word2) { } ``` -Python: +### Python: + ```python class Solution: def minDistance(self, word1: str, word2: str) -> int: @@ -274,7 +275,8 @@ class Solution: return dp[-1][-1] ``` -Go: +### Go: + ```Go func minDistance(word1 string, word2 string) int { m, n := len(word1), len(word2) @@ -310,8 +312,8 @@ func Min(args ...int) int { } ``` +### Javascript: -Javascript: ```javascript const minDistance = (word1, word2) => { let dp = Array.from(Array(word1.length + 1), () => Array(word2.length+1).fill(0)); @@ -338,7 +340,7 @@ const minDistance = (word1, word2) => { }; ``` -TypeScript: +### TypeScript: ```typescript function minDistance(word1: string, word2: string): number { @@ -373,7 +375,7 @@ function minDistance(word1: string, word2: string): number { }; ``` -C: +### C: ```c @@ -405,3 +407,4 @@ int minDistance(char * word1, char * word2){ + diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 7d71b517..8d448739 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -343,8 +343,32 @@ public: ## 其他语言版本 -### Java +### Java: +未剪枝优化 +```java +class Solution { + List> result= new ArrayList<>(); + LinkedList path = new LinkedList<>(); + public List> combine(int n, int k) { + backtracking(n,k,1); + return result; + } + + public void backtracking(int n,int k,int startIndex){ + if (path.size() == k){ + result.add(new ArrayList<>(path)); + return; + } + for (int i =startIndex;i<=n;i++){ + path.add(i); + backtracking(n,k,i+1); + path.removeLast(); + } + } +} +``` +剪枝优化: ```java class Solution { List> result = new ArrayList<>(); diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md index f9a83508..9b76229a 100644 --- a/problems/0084.柱状图中最大的矩形.md +++ b/problems/0084.柱状图中最大的矩形.md @@ -20,7 +20,7 @@ * 1 <= heights.length <=10^5 * 0 <= heights[i] <= 10^4 -# 思路 +## 思路 本题和[42. 接雨水](https://programmercarl.com/0042.接雨水.html),是遥相呼应的两道题目,建议都要仔细做一做,原理上有很多相同的地方,但细节上又有差异,更可以加深对单调栈的理解! @@ -28,7 +28,7 @@ 我们先来看一下暴力解法的解法: -## 暴力解法 +### 暴力解法 ```CPP class Solution { @@ -55,7 +55,7 @@ public: 如上代码并不能通过leetcode,超时了,因为时间复杂度是$O(n^2)$。 -## 双指针解法 +### 双指针解法 本题双指针的写法整体思路和[42. 接雨水](https://programmercarl.com/0042.接雨水.html)是一致的,但要比[42. 接雨水](https://programmercarl.com/0042.接雨水.html)难一些。 @@ -98,7 +98,7 @@ public: }; ``` -## 单调栈 +### 单调栈 本地单调栈的解法和接雨水的题目是遥相呼应的。 @@ -169,7 +169,7 @@ public: } }; -``` +``` 细心的录友会发现,我在 height数组上后,都加了一个元素0, 为什么这么做呢? @@ -229,7 +229,7 @@ public: ## 其他语言版本 -Java: +### Java: 暴力解法: ```java @@ -335,7 +335,7 @@ class Solution { } ``` -Python3: +### Python3: ```python @@ -468,7 +468,7 @@ class Solution: ``` -Go: +### Go: > 单调栈 @@ -506,7 +506,8 @@ func largestRectangleArea(heights []int) int { ``` -JavaScript: +### JavaScript: + ```javascript //双指针 js中运行速度最快 var largestRectangleArea = function(heights) { @@ -581,7 +582,7 @@ var largestRectangleArea = function(heights) { return maxArea; }; ``` -TypeScript: +### TypeScript: > 暴力法(会超时): @@ -669,9 +670,119 @@ function largestRectangleArea(heights: number[]): number { }; ``` +### Rust: + +双指针预处理 +```rust + +impl Solution { + pub fn largest_rectangle_area(v: Vec) -> i32 { + let n = v.len(); + let mut left_smaller_idx = vec![-1; n]; + let mut right_smaller_idx = vec![n as i32; n]; + for i in 1..n { + let mut mid = i as i32 - 1; + while mid >= 0 && v[mid as usize] >= v[i] { + mid = left_smaller_idx[mid as usize]; + } + left_smaller_idx[i] = mid; + } + for i in (0..n-1).rev() { + let mut mid = i + 1; + while mid < n && v[mid] >= v[i] { + mid = right_smaller_idx[mid] as usize; + } + right_smaller_idx[i] = mid as i32; + } + let mut res = 0; + for (idx, &e) in v.iter().enumerate() { + res = res.max((right_smaller_idx[idx] - left_smaller_idx[idx] - 1) * e); + } + dbg!(res) + } +} +``` + +单调栈 +```rust +impl Solution { + pub fn largest_rectangle_area1(mut v: Vec) -> i32 { + v.insert(0, 0); // 便于使第一个元素能够有左侧<=它的值 + v.push(0); // 便于在结束处理最后一个元素后清空残留在栈中的值 + let mut res = 0; + let mut stack = vec![]; // 递增的栈 + for (idx, &e) in v.iter().enumerate() { + while !stack.is_empty() && v[*stack.last().unwrap()] > e { + let pos = stack.pop().unwrap(); + let prev_pos = *stack.last().unwrap(); + let s = (idx - prev_pos - 1) as i32 * v[pos]; + res = res.max(s); + } + stack.push(idx); + } + res + } +} +``` + +Rust + +双指针预处理 +```rust + +impl Solution { + pub fn largest_rectangle_area(v: Vec) -> i32 { + let n = v.len(); + let mut left_smaller_idx = vec![-1; n]; + let mut right_smaller_idx = vec![n as i32; n]; + for i in 1..n { + let mut mid = i as i32 - 1; + while mid >= 0 && v[mid as usize] >= v[i] { + mid = left_smaller_idx[mid as usize]; + } + left_smaller_idx[i] = mid; + } + for i in (0..n-1).rev() { + let mut mid = i + 1; + while mid < n && v[mid] >= v[i] { + mid = right_smaller_idx[mid] as usize; + } + right_smaller_idx[i] = mid as i32; + } + let mut res = 0; + for (idx, &e) in v.iter().enumerate() { + res = res.max((right_smaller_idx[idx] - left_smaller_idx[idx] - 1) * e); + } + dbg!(res) + } +} +``` + +单调栈 +```rust +impl Solution { + pub fn largest_rectangle_area1(mut v: Vec) -> i32 { + v.insert(0, 0); // 便于使第一个元素能够有左侧<=它的值 + v.push(0); // 便于在结束处理最后一个元素后清空残留在栈中的值 + let mut res = 0; + let mut stack = vec![]; // 递增的栈 + for (idx, &e) in v.iter().enumerate() { + while !stack.is_empty() && v[*stack.last().unwrap()] > e { + let pos = stack.pop().unwrap(); + let prev_pos = *stack.last().unwrap(); + let s = (idx - prev_pos - 1) as i32 * v[pos]; + res = res.max(s); + } + stack.push(idx); + } + res + } +} +```

+ diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 88c8bdad..13080cd9 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 90.子集II [力扣题目链接](https://leetcode.cn/problems/subsets-ii/) diff --git a/problems/0096.不同的二叉搜索树.md b/problems/0096.不同的二叉搜索树.md index 368a5747..8d58cc5a 100644 --- a/problems/0096.不同的二叉搜索树.md +++ b/problems/0096.不同的二叉搜索树.md @@ -16,9 +16,9 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210113161941835.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划找到子状态之间的关系很重要!| LeetCode:96.不同的二叉搜索树](https://www.bilibili.com/video/BV1eK411o7QA/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划找到子状态之间的关系很重要!| LeetCode:96.不同的二叉搜索树](https://www.bilibili.com/video/BV1eK411o7QA/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git a/problems/0100.相同的树.md b/problems/0100.相同的树.md index 96acacf6..56a6c884 100644 --- a/problems/0100.相同的树.md +++ b/problems/0100.相同的树.md @@ -19,7 +19,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726173011.png) -# 思路 +## 思路 在[101.对称二叉树](https://programmercarl.com/0101.对称二叉树.html)中,我们讲到对于二叉树是否对称,要比较的是根节点的左子树与右子树是不是相互翻转的,理解这一点就知道了**其实我们要比较的是两个树(这两个树是根节点的左右子树)**,所以在递归遍历的过程中,也是要同时遍历两棵树。 @@ -115,7 +115,7 @@ public: 当然我可以把如上代码整理如下: -## 递归 +### 递归 ```CPP class Solution { @@ -134,7 +134,7 @@ public: }; ``` -## 迭代法 +### 迭代法 ```CPP class Solution { @@ -166,9 +166,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java // 递归法 @@ -205,7 +205,8 @@ class Solution { } } ``` -Python: +### Python: + ```python # 递归法 class Solution: @@ -236,7 +237,8 @@ class Solution: que.append(rightNode.right) return True ``` -Go: +### Go: + > 递归法 ```go func isSameTree(p *TreeNode, q *TreeNode) bool { @@ -258,7 +260,7 @@ func isSameTree(p *TreeNode, q *TreeNode) bool { } ``` -JavaScript: +### JavaScript: > 递归法 @@ -296,7 +298,7 @@ var isSameTree = (p, q) => { }; ``` -TypeScript: +### TypeScript: > 递归法-先序遍历 @@ -341,3 +343,4 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { + diff --git a/problems/0115.不同的子序列.md b/problems/0115.不同的子序列.md index 8c82880d..d925c5de 100644 --- a/problems/0115.不同的子序列.md +++ b/problems/0115.不同的子序列.md @@ -157,8 +157,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public int numDistinct(String s, String t) { @@ -182,7 +182,8 @@ class Solution { } ``` -Python: +### Python: + ```python class Solution: def numDistinct(self, s: str, t: str) -> int: @@ -200,7 +201,8 @@ class Solution: return dp[-1][-1] ``` -Python3: +### Python3: + ```python class SolutionDP2: """ @@ -234,7 +236,8 @@ class SolutionDP2: return dp[-1] ``` -Go: +### Go: + ```go func numDistinct(s string, t string) int { dp:= make([][]int,len(s)+1) @@ -259,8 +262,8 @@ func numDistinct(s string, t string) int { } ``` +### Javascript: -Javascript: ```javascript const numDistinct = (s, t) => { let dp = Array.from(Array(s.length + 1), () => Array(t.length +1).fill(0)); @@ -283,7 +286,7 @@ const numDistinct = (s, t) => { }; ``` -TypeScript: +### TypeScript: ```typescript function numDistinct(s: string, t: string): number { @@ -312,9 +315,8 @@ function numDistinct(s: string, t: string): number { ``` - -

+ diff --git a/problems/0116.填充每个节点的下一个右侧节点指针.md b/problems/0116.填充每个节点的下一个右侧节点指针.md index 31bb6822..003ef75a 100644 --- a/problems/0116.填充每个节点的下一个右侧节点指针.md +++ b/problems/0116.填充每个节点的下一个右侧节点指针.md @@ -30,7 +30,7 @@ struct Node { ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727143202.png) -# 思路 +## 思路 注意题目提示内容,: * 你只能使用常量级额外空间。 @@ -38,7 +38,7 @@ struct Node { 基本上就是要求使用递归了,迭代的方式一定会用到栈或者队列。 -## 递归 +### 递归 一想用递归怎么做呢,虽然层序遍历是最直观的,但是递归的方式确实不好想。 @@ -83,7 +83,7 @@ public: }; ``` -## 迭代(层序遍历) +### 迭代(层序遍历) 本题使用层序遍历是最为直观的,如果对层序遍历不了解,看这篇:[二叉树:层序遍历登场!](https://programmercarl.com/0102.二叉树的层序遍历.html)。 @@ -114,9 +114,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java // 递归法 @@ -169,7 +169,7 @@ class Solution { } ``` -## Python +### Python ```python # 递归法 @@ -210,7 +210,7 @@ class Solution: nodePre.next = None # 本层最后一个节点指向None return root ``` -## Go +### Go ```go // 迭代法 func connect(root *Node) *Node { @@ -259,7 +259,7 @@ func connect(root *Node) *Node { } ``` -## JavaScript +### JavaScript ```js const connect = root => { @@ -287,7 +287,7 @@ const connect = root => { }; ``` -## TypeScript +### TypeScript (注:命名空间‘Node’与typescript中内置类型冲突,这里改成了‘NodePro’) @@ -365,3 +365,4 @@ function connect(root: NodePro | null): NodePro | null { + diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index 06305156..cbdf40e8 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -24,11 +24,9 @@ * 输出:0 解释:在这种情况下, 没有交易完成, 所以最大利润为 0。 -# 算法公开课 - -**《代码随想录》算法视频公开课:[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - +## 算法公开课 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -202,7 +200,7 @@ public: ## 其他语言版本 -Java: +### Java: > 贪心法: @@ -294,8 +292,7 @@ class Solution { ``` - -Python: +### Python: > 贪心法: ```python @@ -351,7 +348,8 @@ class Solution: return dp1 ``` -Go: +### Go: + > 贪心法: ```Go func maxProfit(prices []int) int { @@ -418,7 +416,7 @@ func max(a, b int) int { } ``` -JavaScript: +### JavaScript: > 动态规划 @@ -454,7 +452,7 @@ var maxProfit = function(prices) { }; ``` -TypeScript: +### TypeScript: > 贪心法 @@ -492,7 +490,7 @@ function maxProfit(prices: number[]): number { }; ``` -C#: +### C#: > 贪心法 @@ -533,7 +531,7 @@ public class Solution } ``` -Rust: +### Rust: > 贪心 diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 89c654fa..2c2ab225 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -37,9 +37,9 @@ - 1 <= prices.length <= 3 \* 10 ^ 4 - 0 <= prices[i] <= 10 ^ 4 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法也能解决股票问题!LeetCode:122.买卖股票最佳时机 II](https://www.bilibili.com/video/BV1ev4y1C7na),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法也能解决股票问题!LeetCode:122.买卖股票最佳时机 II](https://www.bilibili.com/video/BV1ev4y1C7na),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -316,7 +316,7 @@ function maxProfit(prices: number[]): number { } ``` -### Rust +### Rust: 贪心: @@ -389,7 +389,7 @@ int maxProfit(int* prices, int pricesSize){ } ``` -### Scala +### Scala: 贪心: @@ -411,3 +411,4 @@ object Solution { + diff --git a/problems/0122.买卖股票的最佳时机II(动态规划).md b/problems/0122.买卖股票的最佳时机II(动态规划).md index 02f8d287..6e08b57c 100644 --- a/problems/0122.买卖股票的最佳时机II(动态规划).md +++ b/problems/0122.买卖股票的最佳时机II(动态规划).md @@ -34,9 +34,9 @@ * 1 <= prices.length <= 3 * 10 ^ 4 * 0 <= prices[i] <= 10 ^ 4 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -133,8 +133,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java // 动态规划 class Solution @@ -191,7 +191,7 @@ class Solution { } ``` -Python: +### Python: > 版本一: ```python @@ -221,7 +221,8 @@ class Solution: return dp[(length-1) % 2][1] ``` -Go: +### Go: + ```go // 买卖股票的最佳时机Ⅱ 动态规划 // 时间复杂度:O(n) 空间复杂度:O(n) @@ -250,7 +251,8 @@ func max(a, b int) int { } ``` -Javascript: +### JavaScript: + ```javascript // 方法一:动态规划(dp 数组) const maxProfit = (prices) => { @@ -290,7 +292,7 @@ const maxProfit = (prices) => { } ``` -TypeScript: +### TypeScript: > 动态规划 @@ -326,7 +328,7 @@ function maxProfit(prices: number[]): number { }; ``` -C#: +### C#: > 贪心法 @@ -363,7 +365,7 @@ public class Solution } ``` -Rust: +### Rust: > 贪心 @@ -414,4 +416,3 @@ impl Solution { - diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md index a646b7d5..72dd9042 100644 --- a/problems/0123.买卖股票的最佳时机III.md +++ b/problems/0123.买卖股票的最佳时机III.md @@ -39,9 +39,9 @@ * 1 <= prices.length <= 10^5 * 0 <= prices[i] <= 10^5 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -221,7 +221,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java // 版本一 @@ -277,7 +277,7 @@ class Solution { } ``` -Python: +### Python: > 版本一: ```python @@ -314,7 +314,7 @@ class Solution: return dp[4] ``` -Go: +### Go: ```go func maxProfit(prices []int) int { @@ -344,7 +344,7 @@ func max(a, b int) int { } ``` -JavaScript: +### JavaScript: > 版本一: @@ -383,7 +383,7 @@ const maxProfit = prices => { }; ``` -TypeScript: +### TypeScript: > 版本一 @@ -413,7 +413,7 @@ function maxProfit(prices: number[]): number { }; ``` -Rust: +### Rust: > 版本一 @@ -465,3 +465,4 @@ impl Solution { + diff --git a/problems/0127.单词接龙.md b/problems/0127.单词接龙.md index 20ad5182..97bc66d0 100644 --- a/problems/0127.单词接龙.md +++ b/problems/0127.单词接龙.md @@ -29,7 +29,7 @@ * 解释:endWord "cog" 不在字典中,所以无法进行转换。 -# 思路 +## 思路 以示例1为例,从这个图中可以看出 hit 到 cog的路线,不止一条,有三条,一条是最短的长度为5,两条长度为6。 @@ -97,9 +97,9 @@ public: 当然本题也可以用双向BFS,就是从头尾两端进行搜索,大家感兴趣,可以自己去实现,这里就不再做详细讲解了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java public int ladderLength(String beginWord, String endWord, List wordList) { @@ -196,7 +196,7 @@ class Solution { } ``` -## Python +### Python ``` class Solution: @@ -221,7 +221,7 @@ class Solution: queue.append(newWord) return 0 ``` -## Go +### Go ```go func ladderLength(beginWord string, endWord string, wordList []string) int { wordMap, que, depth := getWordMap(wordList, beginWord), []string{beginWord}, 0 @@ -274,7 +274,7 @@ func getCandidates(word string) []string { } ``` -## JavaScript +### JavaScript ```javascript var ladderLength = function(beginWord, endWord, wordList) { // 将wordList转成Set,提高查询速度 @@ -310,7 +310,7 @@ var ladderLength = function(beginWord, endWord, wordList) { }; ``` -## TypeScript +### TypeScript ```typescript function ladderLength( beginWord: string, @@ -364,4 +364,3 @@ function diffonechar(word1: string, word2: string): boolean { - diff --git a/problems/0129.求根到叶子节点数字之和.md b/problems/0129.求根到叶子节点数字之和.md index 445e108a..ebb36071 100644 --- a/problems/0129.求根到叶子节点数字之和.md +++ b/problems/0129.求根到叶子节点数字之和.md @@ -10,7 +10,7 @@ [力扣题目链接](https://leetcode.cn/problems/sum-root-to-leaf-numbers/) -# 思路 +## 思路 本题和[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii)是类似的思路,做完这道题,可以顺便把[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii) 和 [112.路径总和](https://programmercarl.com/0112.路径总和.html#_112-路径总和) 做了。 @@ -24,7 +24,7 @@ 那么先按递归三部曲来分析: -## 递归三部曲 +### 递归三部曲 如果对递归三部曲不了解的话,可以看这里:[二叉树:前中后递归详解](https://programmercarl.com/二叉树的递归遍历.html) @@ -116,7 +116,7 @@ path.pop_back(); // 回溯 ``` **把回溯放在花括号外面了,世界上最遥远的距离,是你在花括号里,而我在花括号外!** 这就不对了。 -## 整体C++代码 +整体C++代码 关键逻辑分析完了,整体C++代码如下: @@ -162,16 +162,16 @@ public: }; ``` -# 总结 +## 总结 过于简洁的代码,很容易让初学者忽视了本题中回溯的精髓,甚至作者本身都没有想清楚自己用了回溯。 **我这里提供的代码把整个回溯过程充分体现出来,希望可以帮助大家看的明明白白!** -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java class Solution { @@ -219,7 +219,8 @@ class Solution { } ``` -Python: +### Python: + ```python class Solution: def sumNumbers(self, root: TreeNode) -> int: @@ -246,7 +247,7 @@ class Solution: backtrace(root) return res ``` -Go: +### Go: ```go func sumNumbers(root *TreeNode) int { @@ -271,7 +272,8 @@ func dfs(root *TreeNode, tmpSum int, sum *int) { -JavaScript: +### JavaScript: + ```javascript var sumNumbers = function(root) { const listToInt = path => { @@ -315,7 +317,7 @@ var sumNumbers = function(root) { }; ``` -TypeScript: +### TypeScript: ```typescript function sumNumbers(root: TreeNode | null): number { @@ -351,7 +353,7 @@ function sumNumbers(root: TreeNode | null): number { }; ``` -C: +### C: ```c //sum记录总和 @@ -384,3 +386,4 @@ int sumNumbers(struct TreeNode* root){ + diff --git a/problems/0132.分割回文串II.md b/problems/0132.分割回文串II.md index 9b164dfb..eb91a189 100644 --- a/problems/0132.分割回文串II.md +++ b/problems/0132.分割回文串II.md @@ -34,7 +34,7 @@ * 1 <= s.length <= 2000 * s 仅由小写英文字母组成 -# 思路 +## 思路 我们在讲解回溯法系列的时候,讲过了这道题目[回溯算法:131.分割回文串](https://programmercarl.com/0131.分割回文串.html)。 @@ -201,9 +201,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -257,7 +257,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -286,7 +286,7 @@ class Solution: return dp[-1] ``` -## Go +### Go ```go func minCut(s string) int { @@ -330,7 +330,7 @@ func min(i, j int) int { } ``` -## JavaScript +### JavaScript ```js var minCut = function(s) { @@ -376,3 +376,4 @@ var minCut = function(s) { + diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index ad9acfbc..2f9539e8 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -45,12 +45,14 @@ * 解释: 你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油。开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油。开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油。你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。因此,无论怎样,你都不可能绕环路行驶一周。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,得这么加油才能跑完全程!LeetCode :134.加油站](https://www.bilibili.com/video/BV1jA411r7WX),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,得这么加油才能跑完全程!LeetCode :134.加油站](https://www.bilibili.com/video/BV1jA411r7WX),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + +## 思路 -## 暴力方法 +### 暴力方法 暴力的方法很明显就是O(n^2)的,遍历每一个加油站为起点的情况,模拟一圈。 @@ -85,7 +87,7 @@ public: * 空间复杂度:O(1) -## 贪心算法(方法一) +### 贪心算法(方法一) 直接从全局进行贪心选择,情况如下: @@ -134,7 +136,7 @@ public: 但不管怎么说,解法毕竟还是巧妙的,不用过于执着于其名字称呼。 -## 贪心算法(方法二) +### 贪心算法(方法二) 可以换一个思路,首先如果总油量减去总消耗大于等于零那么一定可以跑完一圈,说明 各个站点的加油站 剩油量rest[i]相加一定是大于等于零的。 @@ -633,3 +635,4 @@ object Solution { + diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md index cf3ccc8e..d130bd68 100644 --- a/problems/0135.分发糖果.md +++ b/problems/0135.分发糖果.md @@ -28,9 +28,9 @@ * 输出: 4 * 解释: 你可以分别给这三个孩子分发 1、2、1 颗糖果。第三个孩子只得到 1 颗糖果,这已满足上述两个条件。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,两者兼顾很容易顾此失彼!LeetCode:135.分发糖果](https://www.bilibili.com/video/BV1ev4y1r7wN),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,两者兼顾很容易顾此失彼!LeetCode:135.分发糖果](https://www.bilibili.com/video/BV1ev4y1r7wN),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -234,7 +234,7 @@ func findMax(num1 int, num2 int) int { } ``` -### Javascript: +### Javascript ```Javascript var candy = function(ratings) { let candys = new Array(ratings.length).fill(1) @@ -376,3 +376,4 @@ object Solution { + diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index 0d88ba36..d93288ae 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -33,9 +33,9 @@ * 输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] * 输出: false -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[你的背包如何装满?| LeetCode:139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你的背包如何装满?| LeetCode:139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -123,7 +123,7 @@ public: **这个代码就可以AC了,当然回溯算法不是本题的主菜,背包才是!** -## 背包问题 +### 背包问题 单词就是物品,字符串s就是背包,单词能否组成字符串s,就是问物品能不能把背包装满。 @@ -239,7 +239,7 @@ public: } }; -``` +``` 使用用例:s = "applepenapple", wordDict = ["apple", "pen"],对应的dp数组状态如下: @@ -259,8 +259,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public boolean wordBreak(String s, List wordDict) { @@ -335,7 +335,7 @@ class Solution { } ``` -Python: +### Python: 回溯 ```python @@ -397,7 +397,8 @@ class Solution: -Go: +### Go: + ```Go func wordBreak(s string,wordDict []string) bool { wordDictSet := make(map[string]bool) @@ -433,7 +434,8 @@ func wordBreak(s string, wordDict []string) bool { } ``` -Javascript: +### JavaScript: + ```javascript const wordBreak = (s, wordDict) => { @@ -454,7 +456,7 @@ const wordBreak = (s, wordDict) => { } ``` -TypeScript: +### TypeScript: > 动态规划 @@ -496,7 +498,7 @@ function wordBreak(s: string, wordDict: string[]): boolean { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -519,3 +521,4 @@ impl Solution { + diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 19ccb725..0c1a526f 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -433,7 +433,7 @@ class Solution { } ``` -### python: +### Python: (版本一)先删除空白,然后整个反转,最后单词反转。 **因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)** @@ -547,26 +547,28 @@ func reverseWords(s string) string { b = b[:slowIndex] } //2.反转整个字符串 - reverse(&b, 0, len(b)-1) + reverse(b) //3.反转单个单词 i单词开始位置,j单词结束位置 i := 0 for i < len(b) { j := i for ; j < len(b) && b[j] != ' '; j++ { } - reverse(&b, i, j-1) + reverse(b[i:j]) i = j i++ } return string(b) } -func reverse(b *[]byte, left, right int) { - for left < right { - (*b)[left], (*b)[right] = (*b)[right], (*b)[left] - left++ - right-- - } +func reverse(b []byte) { + left := 0 + right := len(b) - 1 + for left < right { + b[left], b[right] = b[right], b[left] + left++ + right-- + } } ``` @@ -974,4 +976,3 @@ char * reverseWords(char * s){ - diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 773a910a..e4c5c484 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -31,9 +31,9 @@ * 0 <= prices.length <= 1000 * 0 <= prices[i] <= 1000 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -173,7 +173,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java // 版本一: 三维 dp数组 @@ -295,7 +295,7 @@ class Solution { } ``` -Python: +### Python: 版本一 @@ -329,7 +329,7 @@ class Solution: dp[j] = max(dp[j],dp[j-1]+prices[i]) return dp[2*k] ``` -Go: +### Go: 版本一: @@ -404,7 +404,7 @@ func max188(a, b int) int { } ``` -Javascript: +### JavaScript: ```javascript // 方法一:动态规划 @@ -454,7 +454,7 @@ var maxProfit = function(k, prices) { }; ``` -TypeScript: +### TypeScript: ```typescript function maxProfit(k: number, prices: number[]): number { @@ -474,7 +474,7 @@ function maxProfit(k: number, prices: number[]): number { }; ``` -Rust: +### Rust: ```rust impl Solution { diff --git a/problems/0189.旋转数组.md b/problems/0189.旋转数组.md index 35819694..d60612e9 100644 --- a/problems/0189.旋转数组.md +++ b/problems/0189.旋转数组.md @@ -33,7 +33,7 @@ 向右旋转 2 步: [3,99,-1,-100]。 -# 思路 +## 思路 这道题目在字符串里其实很常见,我把字符串反转相关的题目列一下: @@ -83,9 +83,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -106,7 +106,7 @@ class Solution { } ``` -## Python +### Python 方法一:局部翻转 + 整体翻转 ```python @@ -139,7 +139,7 @@ class Solution: # 备注:这个方法会导致空间复杂度变成 O(n) 因为我们要创建一个 copy 数组。但是不失为一种思路。 ``` -## Go +### Go ```go func rotate(nums []int, k int) { @@ -157,7 +157,7 @@ func reverse(nums []int){ } ``` -## JavaScript +### JavaScript ```js var rotate = function (nums, k) { @@ -178,7 +178,7 @@ var rotate = function (nums, k) { }; ``` -## TypeScript +### TypeScript ```typescript function rotate(nums: number[], k: number): void { @@ -205,3 +205,4 @@ function reverseByRange(nums: number[], left: number, right: number): void { + diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index 80902559..a7bc4c99 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -31,9 +31,9 @@ * 0 <= nums.length <= 100 * 0 <= nums[i] <= 400 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -121,8 +121,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java // 动态规划 class Solution { @@ -194,7 +194,7 @@ class Solution { } ``` -Python: +### Python: 1维DP ```python @@ -255,7 +255,8 @@ class Solution: ``` -Go: +### Go: + ```Go func rob(nums []int) int { n := len(nums) @@ -275,7 +276,7 @@ func max(a, b int) int { } ``` -JavaScript: +### JavaScript: ```javascript const rob = nums => { @@ -291,7 +292,7 @@ const rob = nums => { }; ``` -TypeScript: +### TypeScript: ```typescript function rob(nums: number[]): number { @@ -314,7 +315,7 @@ function rob(nums: number[]): number { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -338,4 +339,3 @@ impl Solution { - diff --git a/problems/0200.岛屿数量.广搜版.md b/problems/0200.岛屿数量.广搜版.md index c20fe4f1..cd3ae70d 100644 --- a/problems/0200.岛屿数量.广搜版.md +++ b/problems/0200.岛屿数量.广搜版.md @@ -197,7 +197,6 @@ class Solution { } ``` -## 其他语言版本 ### Python BFS solution ```python @@ -244,3 +243,4 @@ class Solution: ``` + diff --git a/problems/0205.同构字符串.md b/problems/0205.同构字符串.md index a507638c..e07ab746 100644 --- a/problems/0205.同构字符串.md +++ b/problems/0205.同构字符串.md @@ -29,7 +29,7 @@ 提示:可以假设 s 和 t 长度相同。 -# 思路 +## 思路 字符串没有说都是小写字母之类的,所以用数组不合适了,用map来做映射。 @@ -61,9 +61,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -87,7 +87,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -110,7 +110,7 @@ class Solution: return True ``` -## Go +### Go ```go func isIsomorphic(s string, t string) bool { @@ -132,7 +132,7 @@ func isIsomorphic(s string, t string) bool { } ``` -## JavaScript +### JavaScript ```js var isIsomorphic = function(s, t) { @@ -156,7 +156,7 @@ var isIsomorphic = function(s, t) { }; ``` -## TypeScript +### TypeScript ```typescript function isIsomorphic(s: string, t: string): boolean { @@ -183,3 +183,4 @@ function isIsomorphic(s: string, t: string): boolean { + diff --git a/problems/0213.打家劫舍II.md b/problems/0213.打家劫舍II.md index ee62b574..cd9d596d 100644 --- a/problems/0213.打家劫舍II.md +++ b/problems/0213.打家劫舍II.md @@ -31,9 +31,9 @@ * 1 <= nums.length <= 100 * 0 <= nums[i] <= 1000 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -104,8 +104,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int rob(int[] nums) { @@ -129,7 +129,7 @@ class Solution { } ``` -Python: +### Python: ```Python class Solution: @@ -219,7 +219,7 @@ class Solution: ``` -Go: +### Go: ```go // 打家劫舍Ⅱ 动态规划 @@ -257,7 +257,8 @@ func max(a, b int) int { } ``` -javascipt: +### JavaScript: + ```javascript var rob = function(nums) { const n = nums.length @@ -279,7 +280,7 @@ const robRange = (nums, start, end) => { return dp[end] } ``` -TypeScript: +### TypeScript: ```typescript function rob(nums: number[]): number { @@ -301,7 +302,7 @@ function robRange(nums: number[], start: number, end: number): number { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -336,3 +337,4 @@ impl Solution { + diff --git a/problems/0234.回文链表.md b/problems/0234.回文链表.md index 18b397e3..fef942fc 100644 --- a/problems/0234.回文链表.md +++ b/problems/0234.回文链表.md @@ -432,3 +432,4 @@ function reverseList(head: ListNode | null): ListNode | null { + diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index 70ab8649..a0c138ee 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -28,9 +28,9 @@ 提示: * 1 <= n <= 10^4 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[换汤不换药!| LeetCode:279.完全平方数](https://www.bilibili.com/video/BV12P411T7Br/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[换汤不换药!| LeetCode:279.完全平方数](https://www.bilibili.com/video/BV12P411T7Br/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -106,8 +106,6 @@ dp[5] = min(dp[4] + 1, dp[1] + 1) = 2 最后的dp[n]为最终结果。 -## C++代码 - 以上动规五部曲分析完毕C++代码如下: ```CPP @@ -165,8 +163,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { // 版本一,先遍历物品, 再遍历背包 @@ -219,7 +217,7 @@ class Solution { } ``` -Python: +### Python: 先遍历物品, 再遍历背包 ```python @@ -276,7 +274,8 @@ class Solution: ``` -Go: +### Go: + ```go // 版本一,先遍历物品, 再遍历背包 func numSquares1(n int) int { @@ -327,7 +326,8 @@ func min(a, b int) int { } ``` -Javascript: +### Javascript: + ```Javascript // 先遍历物品,再遍历背包 var numSquares1 = function(n) { @@ -357,7 +357,7 @@ var numSquares2 = function(n) { }; ``` -TypeScript: +### TypeScript: ```typescript // 先遍历物品 @@ -389,7 +389,7 @@ function numSquares(n: number): number { }; ``` -Rust: +### Rust: ```rust // 先遍历背包 @@ -439,3 +439,4 @@ impl Solution { + diff --git a/problems/0283.移动零.md b/problems/0283.移动零.md index 22d6428c..42232cc0 100644 --- a/problems/0283.移动零.md +++ b/problems/0283.移动零.md @@ -4,9 +4,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-# 动态规划:一样的套路,再求一次完全平方数 - -# 283. 移动零 +# 283. 移动零:动态规划:一样的套路,再求一次完全平方数 [力扣题目链接](https://leetcode.cn/problems/move-zeroes/) @@ -22,7 +20,7 @@ 尽量减少操作次数。 -# 思路 +## 思路 做这道题目之前,大家可以做一做[27.移除元素](https://programmercarl.com/0027.移除元素.html) @@ -58,9 +56,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java public void moveZeroes(int[] nums) { @@ -77,7 +75,7 @@ public void moveZeroes(int[] nums) { } ``` -Python: +### Python: ```python def moveZeroes(self, nums: List[int]) -> None: @@ -100,7 +98,7 @@ Python: fast += 1 ``` -Go: +### Go: ```go func moveZeroes(nums []int) { @@ -116,7 +114,8 @@ func moveZeroes(nums []int) { } ``` -JavaScript: +### JavaScript: + ```javascript var moveZeroes = function(nums) { let slow = 0; @@ -133,7 +132,7 @@ var moveZeroes = function(nums) { }; ``` -TypeScript: +### TypeScript: ```typescript function moveZeroes(nums: number[]): void { @@ -159,3 +158,4 @@ function moveZeroes(nums: number[]): void { + diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md index c58c3bf6..11cf13d9 100644 --- a/problems/0300.最长上升子序列.md +++ b/problems/0300.最长上升子序列.md @@ -33,7 +33,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列问题,元素不连续!| LeetCode:300.最长递增子序列](https://www.bilibili.com/video/BV1ng411J7xP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[动态规划之子序列问题,元素不连续!| LeetCode:300.最长递增子序列](https://www.bilibili.com/video/BV1ng411J7xP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -124,8 +124,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int lengthOfLIS(int[] nums) { @@ -147,7 +147,7 @@ class Solution { } ``` -Python: +### Python: DP ```python @@ -189,7 +189,8 @@ class Solution: return len(tails) # 返回递增子序列的长度 ``` -Go: +### Go: + ```go // 动态规划求解 func lengthOfLIS(nums []int) int { @@ -248,7 +249,8 @@ func lengthOfLIS(nums []int ) int { } ``` -Javascript +### Javascript: + ```javascript const lengthOfLIS = (nums) => { let dp = Array(nums.length).fill(1); @@ -267,7 +269,7 @@ const lengthOfLIS = (nums) => { }; ``` -TypeScript +### TypeScript: ```typescript function lengthOfLIS(nums: number[]): number { @@ -288,7 +290,8 @@ function lengthOfLIS(nums: number[]): number { }; ``` -Rust: +### Rust: + ```rust pub fn length_of_lis(nums: Vec) -> i32 { let mut dp = vec![1; nums.len() + 1]; @@ -307,13 +310,8 @@ pub fn length_of_lis(nums: Vec) -> i32 { - - - - - -

+ diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index cd71136b..f4093b67 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.md @@ -20,9 +20,9 @@ * 输出: 3 * 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -174,7 +174,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java class Solution { @@ -273,8 +273,9 @@ class Solution { } ``` -Python: +### Python: 版本一 + ```python from typing import List @@ -319,7 +320,8 @@ class Solution: return max(dp[-1][1], dp[-1][2]) ``` -Go: +### Go: + ```go // 最佳买卖股票时机含冷冻期 动态规划 // 时间复杂度O(n) 空间复杂度O(n) @@ -355,7 +357,7 @@ func max(a, b int) int { } ``` -Javascript: +### Javascript: ```javascript const maxProfit = (prices) => { @@ -397,7 +399,7 @@ const maxProfit = (prices) => { }; ``` -TypeScript: +### TypeScript: > 版本一,与本文思路一致 @@ -455,7 +457,7 @@ function maxProfit(prices: number[]): number { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -484,3 +486,4 @@ impl Solution { + diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index 1f3f4df2..f32fd13e 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -39,9 +39,9 @@ * 1 <= coins[i] <= 2^31 - 1 * 0 <= amount <= 10^4 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满背包最少的物品件数是多少?| LeetCode:322.零钱兑换](https://www.bilibili.com/video/BV14K411R7yv/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包最少的物品件数是多少?| LeetCode:322.零钱兑换](https://www.bilibili.com/video/BV14K411R7yv/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 @@ -110,7 +110,6 @@ dp[0] = 0; dp[amount]为最终结果。 -## C++代码 以上分析完毕,C++ 代码如下: ```CPP @@ -187,8 +186,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int coinChange(int[] coins, int amount) { @@ -215,7 +214,7 @@ class Solution { } ``` -Python: +### Python: 先遍历物品 后遍历背包 @@ -288,7 +287,8 @@ class Solution: ``` -Go: +### Go: + ```go // 版本一, 先遍历物品,再遍历背包 func coinChange1(coins []int, amount int) int { @@ -352,7 +352,7 @@ func min(a, b int) int { ``` -Rust: +### Rust: ```rust // 遍历物品 @@ -398,7 +398,8 @@ impl Solution { } ``` -Javascript: +### Javascript: + ```javascript // 遍历物品 const coinChange = (coins, amount) => { @@ -435,7 +436,7 @@ var coinChange = function(coins, amount) { } ``` -TypeScript: +### TypeScript: ```typescript // 遍历物品 @@ -473,3 +474,4 @@ function coinChange(coins: number[], amount: number): number { + diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md index 3798b48d..2795e313 100644 --- a/problems/0332.重新安排行程.md +++ b/problems/0332.重新安排行程.md @@ -31,7 +31,7 @@ ## 算法公开课 -**如果对回溯算法基础还不了解的话,我还特意录制了一期视频,[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/)** 可以结合题解和视频一起看,希望对大家理解回溯算法有所帮助。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/) ,相信结合视频再看本篇题解,更有助于大家对本题的理解。** ## 思路 @@ -793,4 +793,3 @@ impl Solution { - diff --git a/problems/0337.打家劫舍III.md b/problems/0337.打家劫舍III.md index 1708b7a1..96d487e2 100644 --- a/problems/0337.打家劫舍III.md +++ b/problems/0337.打家劫舍III.md @@ -16,9 +16,9 @@ ![337.打家劫舍III](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223173849619.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,房间连成树了,偷不偷呢?| LeetCode:337.打家劫舍3](https://www.bilibili.com/video/BV1H24y1Q7sY),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,房间连成树了,偷不偷呢?| LeetCode:337.打家劫舍3](https://www.bilibili.com/video/BV1H24y1Q7sY),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -523,3 +523,4 @@ impl Solution { + diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index 3ff8dedb..cba82f6c 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -22,9 +22,9 @@ * 说明: 你可以假设 n 不小于 2 且不大于 58。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,本题关键在于理解递推公式!| LeetCode:343. 整数拆分](https://www.bilibili.com/video/BV1Mg411q7YJ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,本题关键在于理解递推公式!| LeetCode:343. 整数拆分](https://www.bilibili.com/video/BV1Mg411q7YJ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -473,3 +473,4 @@ object Solution { + diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 08de23ae..943dfe39 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -33,11 +33,13 @@ - 输入: [1,2,3,4,5,6,7,8,9] - 输出: 2 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,寻找摆动有细节!| LeetCode:376.摆动序列](https://www.bilibili.com/video/BV17M411b7NS),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,寻找摆动有细节!| LeetCode:376.摆动序列](https://www.bilibili.com/video/BV17M411b7NS),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -## 思路 1(贪心解法) +## 思路 + +### 思路 1(贪心解法) 本题要求通过从原始序列中删除一些(也可以不删除)元素来获得子序列,剩下的元素保持其原始顺序。 @@ -69,7 +71,7 @@ 2. 情况二:数组首尾两端 3. 情况三:单调坡中有平坡 -### 情况一:上下坡中有平坡 +#### 情况一:上下坡中有平坡 例如 [1,2,2,2,1]这样的数组,如图: @@ -87,7 +89,7 @@ 所以我们记录峰值的条件应该是: `(preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0)`,为什么这里允许 prediff == 0 ,就是为了 上面我说的这种情况。 -### 情况二:数组首尾两端 +#### 情况二:数组首尾两端 所以本题统计峰值的时候,数组最左面和最右面如何统计呢? @@ -142,7 +144,7 @@ public: 所以此时我们要讨论情况三! -### 情况三:单调坡度有平坡 +#### 情况三:单调坡度有平坡 在版本一中,我们忽略了一种情况,即 如果在一个单调坡度上有平坡,例如[1,2,2,2,3,4],如图: @@ -187,7 +189,7 @@ public: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230108174452.png) -## 思路 2(动态规划) +### 思路 2(动态规划) 考虑用动态规划的思想来解决这个问题。 @@ -696,4 +698,3 @@ object Solution { - diff --git a/problems/0377.组合总和Ⅳ.md b/problems/0377.组合总和Ⅳ.md index bd43d526..d9699c54 100644 --- a/problems/0377.组合总和Ⅳ.md +++ b/problems/0377.组合总和Ⅳ.md @@ -31,9 +31,9 @@ 因此输出为 7。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满背包有几种方法?求排列数?| LeetCode:377.组合总和IV](https://www.bilibili.com/video/BV1V14y1n7B6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有几种方法?求排列数?| LeetCode:377.组合总和IV](https://www.bilibili.com/video/BV1V14y1n7B6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -154,8 +154,7 @@ C++测试用例有两个数相加超过int的数据,所以需要在if里加上 ## 其他语言版本 - -Java: +### Java: ```Java class Solution { @@ -174,7 +173,7 @@ class Solution { } ``` -Python: +### Python: 卡哥版 @@ -207,7 +206,8 @@ class Solution: ``` -Go: +### Go: + ```go func combinationSum4(nums []int, target int) int { //定义dp数组 @@ -226,7 +226,8 @@ func combinationSum4(nums []int, target int) int { } ``` -Javascript: +### Javascript: + ```javascript const combinationSum4 = (nums, target) => { @@ -245,7 +246,7 @@ const combinationSum4 = (nums, target) => { }; ``` -TypeScript: +### TypeScript: ```typescript function combinationSum4(nums: number[], target: number): number { @@ -264,7 +265,7 @@ function combinationSum4(nums: number[], target: number): number { }; ``` -Rust +### Rust: ```Rust impl Solution { @@ -289,3 +290,4 @@ impl Solution { + diff --git a/problems/0392.判断子序列.md b/problems/0392.判断子序列.md index c10114c0..6342a41f 100644 --- a/problems/0392.判断子序列.md +++ b/problems/0392.判断子序列.md @@ -28,9 +28,9 @@ 两个字符串都只由小写字符组成。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,用相似思路解决复杂问题 | LeetCode:392.判断子序列](https://www.bilibili.com/video/BV1tv4y1B7ym/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,用相似思路解决复杂问题 | LeetCode:392.判断子序列](https://www.bilibili.com/video/BV1tv4y1B7ym/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -149,8 +149,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public boolean isSubsequence(String s, String t) { @@ -174,7 +174,8 @@ class Solution { } ``` -Python: +### Python: + ```python class Solution: def isSubsequence(self, s: str, t: str) -> bool: @@ -190,7 +191,7 @@ class Solution: return False ``` -JavaScript: +### JavaScript: ```javascript const isSubsequence = (s, t) => { @@ -213,7 +214,7 @@ const isSubsequence = (s, t) => { }; ``` -TypeScript: +### TypeScript: ```typescript function isSubsequence(s: string, t: string): boolean { @@ -237,7 +238,7 @@ function isSubsequence(s: string, t: string): boolean { }; ``` -Go: +### Go: ```go func isSubsequence(s string, t string) bool { @@ -266,3 +267,4 @@ func isSubsequence(s string, t string) bool { + diff --git a/problems/0406.根据身高重建队列.md b/problems/0406.根据身高重建队列.md index 60d1fcab..9cd78fac 100644 --- a/problems/0406.根据身高重建队列.md +++ b/problems/0406.根据身高重建队列.md @@ -37,9 +37,9 @@ 题目数据确保队列可以被重建 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,不要两边一起贪,会顾此失彼 | LeetCode:406.根据身高重建队列](https://www.bilibili.com/video/BV1EA411675Y),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,不要两边一起贪,会顾此失彼 | LeetCode:406.根据身高重建队列](https://www.bilibili.com/video/BV1EA411675Y),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -402,3 +402,4 @@ object Solution { + diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index a886b99a..2b2be103 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -4,7 +4,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-## 416. 分割等和子集 +# 416. 分割等和子集 [力扣题目链接](https://leetcode.cn/problems/partition-equal-subset-sum/) @@ -30,9 +30,9 @@ * 1 <= nums.length <= 200 * 1 <= nums[i] <= 100 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之背包问题,这个包能装满吗?| LeetCode:416.分割等和子集](https://www.bilibili.com/video/BV1rt4y1N7jE/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之背包问题,这个包能装满吗?| LeetCode:416.分割等和子集](https://www.bilibili.com/video/BV1rt4y1N7jE/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -53,7 +53,7 @@ * [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) * [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) -## 01背包问题 +### 01背包问题 背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。 @@ -479,7 +479,7 @@ func canPartition(nums []int) bool { } ``` -### javaScript: +### JavaScript: ```js var canPartition = function(nums) { @@ -499,7 +499,7 @@ var canPartition = function(nums) { ``` -### Rust +### Rust: ```Rust impl Solution { @@ -681,7 +681,7 @@ function canPartition(nums: number[]): boolean { }; ``` -### Scala +### Scala: 滚动数组: ```scala diff --git a/problems/0435.无重叠区间.md b/problems/0435.无重叠区间.md index 61f42d42..c307532e 100644 --- a/problems/0435.无重叠区间.md +++ b/problems/0435.无重叠区间.md @@ -30,9 +30,9 @@ * 输出: 0 * 解释: 你不需要移除任何区间,因为它们已经是无重叠的了。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,依然是判断重叠区间 | LeetCode:435.无重叠区间](https://www.bilibili.com/video/BV1A14y1c7E1),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,依然是判断重叠区间 | LeetCode:435.无重叠区间](https://www.bilibili.com/video/BV1A14y1c7E1),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -89,9 +89,9 @@ public: 大家此时会发现如此复杂的一个问题,代码实现却这么简单! +## 补充 - -## 补充(1) +### 补充(1) 左边界排序可不可以呢? @@ -144,7 +144,7 @@ public: ``` -## 补充(2) +### 补充(2) 本题其实和[452.用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)非常像,弓箭的数量就相当于是非交叉区间的数量,只要把弓箭那道题目代码里射爆气球的判断条件加个等号(认为[0,1][1,2]不是相邻区间),然后用总区间数减去弓箭数量 就是要移除的区间数量了。 @@ -311,7 +311,7 @@ func min(a, b int) int { } ``` -### Javascript: +### Javascript - 按右边界排序 ```Javascript var eraseOverlapIntervals = function(intervals) { @@ -447,3 +447,4 @@ impl Solution { + diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 13599fd8..18e5cb4c 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -268,6 +268,34 @@ public: ### Java +```java +// 解法1(最好理解的版本) +class Solution { + public TreeNode deleteNode(TreeNode root, int key) { + if (root == null) return root; + if (root.val == key) { + if (root.left == null) { + return root.right; + } else if (root.right == null) { + return root.left; + } else { + TreeNode cur = root.right; + while (cur.left != null) { + cur = cur.left; + } + cur.left = root.left; + root = root.right; + return root; + } + } + if (root.val > key) root.left = deleteNode(root.left, key); + if (root.val < key) root.right = deleteNode(root.right, key); + return root; + } +} +``` + + ```java class Solution { public TreeNode deleteNode(TreeNode root, int key) { @@ -296,34 +324,59 @@ class Solution { } } ``` +递归法 ```java -// 解法2 class Solution { public TreeNode deleteNode(TreeNode root, int key) { - if (root == null) return root; - if (root.val == key) { - if (root.left == null) { - return root.right; - } else if (root.right == null) { - return root.left; - } else { - TreeNode cur = root.right; - while (cur.left != null) { - cur = cur.left; - } - cur.left = root.left; - root = root.right; - return root; + if (root == null){ + return null; + } + //寻找对应的对应的前面的节点,以及他的前一个节点 + TreeNode cur = root; + TreeNode pre = null; + while (cur != null){ + if (cur.val < key){ + pre = cur; + cur = cur.right; + } else if (cur.val > key) { + pre = cur; + cur = cur.left; + }else { + break; } } - if (root.val > key) root.left = deleteNode(root.left, key); - if (root.val < key) root.right = deleteNode(root.right, key); + if (pre == null){ + return deleteOneNode(cur); + } + if (pre.left !=null && pre.left.val == key){ + pre.left = deleteOneNode(cur); + } + if (pre.right !=null && pre.right.val == key){ + pre.right = deleteOneNode(cur); + } return root; } + + public TreeNode deleteOneNode(TreeNode node){ + if (node == null){ + return null; + } + if (node.right == null){ + return node.left; + } + TreeNode cur = node.right; + while (cur.left !=null){ + cur = cur.left; + } + cur.left = node.left; + return node.right; + } } ``` + ### Python + 递归法(版本一) ```python class Solution: diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index ff476f41..90cd7085 100644 --- a/problems/0452.用最少数量的箭引爆气球.md +++ b/problems/0452.用最少数量的箭引爆气球.md @@ -42,9 +42,9 @@ * points[i].length == 2 * -2^31 <= xstart < xend <= 2^31 - 1 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,判断重叠区间问题 | LeetCode:452.用最少数量的箭引爆气球](https://www.bilibili.com/video/BV1SA41167xe),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,判断重叠区间问题 | LeetCode:452.用最少数量的箭引爆气球](https://www.bilibili.com/video/BV1SA41167xe),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index ce1987ef..c9c1a852 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -30,9 +30,9 @@ - 0 <= s.length <= 3 \* 10^4 - 1 <= g[i], s[j] <= 2^31 - 1 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,你想先喂哪个小孩?| LeetCode:455.分发饼干](https://www.bilibili.com/video/BV1MM411b7cq),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,你想先喂哪个小孩?| LeetCode:455.分发饼干](https://www.bilibili.com/video/BV1MM411b7cq),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git a/problems/0463.岛屿的周长.md b/problems/0463.岛屿的周长.md index 18f1d01e..14fa98dc 100644 --- a/problems/0463.岛屿的周长.md +++ b/problems/0463.岛屿的周长.md @@ -90,7 +90,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java // 解法一 @@ -191,8 +191,8 @@ class Solution { ``` -Python: -### 解法1: +### Python: + 扫描每个cell,如果当前位置为岛屿 grid[i][j] == 1, 从当前位置判断四边方向,如果边界或者是水域,证明有边界存在,res矩阵的对应cell加一。 ```python @@ -228,7 +228,8 @@ class Solution: ``` -Go: +### Go: + ```go func islandPerimeter(grid [][]int) int { m, n := len(grid), len(grid[0]) @@ -249,7 +250,8 @@ func islandPerimeter(grid [][]int) int { } ``` -JavaScript: +### JavaScript: + ```javascript //解法一 var islandPerimeter = function(grid) { @@ -305,3 +307,4 @@ var islandPerimeter = function(grid) { + diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index 7c1206ef..8f6197ac 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -34,9 +34,9 @@ * strs[i] 仅由 '0' 和 '1' 组成 * 1 <= m, n <= 100 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满这个背包最多用多少个物品?| LeetCode:474.一和零](https://www.bilibili.com/video/BV1rW4y1x7ZQ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满这个背包最多用多少个物品?| LeetCode:474.一和零](https://www.bilibili.com/video/BV1rW4y1x7ZQ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -538,3 +538,4 @@ impl Solution { + diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 1902d5ed..4a4e966c 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -37,9 +37,9 @@ * 初始的数组的和不会超过 1000 。 * 保证返回的最终结果能被 32 位整数存下。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满背包有多少种方法?| LeetCode:494.目标和](https://www.bilibili.com/video/BV1o8411j73x/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有多少种方法?| LeetCode:494.目标和](https://www.bilibili.com/video/BV1o8411j73x/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -67,7 +67,7 @@ target是固定的,sum是固定的,left就可以求出来。 此时问题就是在集合nums中找出和为left的组合。 -## 回溯算法 +### 回溯算法 在回溯算法系列中,一起学过这道题目[回溯算法:39. 组合总和](https://programmercarl.com/0039.组合总和.html)的录友应该感觉很熟悉,这不就是组合总和问题么? @@ -118,7 +118,7 @@ public: 也可以使用记忆化回溯,但这里我就不在回溯上下功夫了,直接看动规吧 -## 动态规划 +### 动态规划 如何转化为01背包问题呢。 @@ -519,8 +519,6 @@ const findTargetSumWays = (nums, target) => { ### TypeScript -TypeScript: - ```ts function findTargetSumWays(nums: number[], target: number): number { // 把数组分成两个组合left, right.left + right = sum, left - right = target. @@ -590,3 +588,4 @@ impl Solution { + diff --git a/problems/0496.下一个更大元素I.md b/problems/0496.下一个更大元素I.md index 31c3ce43..6bcafafb 100644 --- a/problems/0496.下一个更大元素I.md +++ b/problems/0496.下一个更大元素I.md @@ -37,7 +37,7 @@ nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位 * nums1和nums2中所有整数 互不相同 * nums1 中的所有整数同样出现在 nums2 中 -# 思路 +## 思路 做本题之前,建议先做一下[739. 每日温度](https://programmercarl.com/0739.每日温度.html) @@ -191,7 +191,8 @@ public: 建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简! ## 其他语言版本 -Java +### Java + ```java class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { @@ -248,7 +249,8 @@ class Solution { } } ``` -Python3: +### Python3 + ```python class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: @@ -269,7 +271,7 @@ class Solution: return result ``` -Go: +### Go > 未精简版本 ```go @@ -335,7 +337,7 @@ func nextGreaterElement(nums1 []int, nums2 []int) []int { } ``` -JavaScript: +### JavaScript ```JS var nextGreaterElement = function (nums1, nums2) { @@ -358,7 +360,7 @@ var nextGreaterElement = function (nums1, nums2) { }; ``` -TypeScript: +### TypeScript ```typescript function nextGreaterElement(nums1: number[], nums2: number[]): number[] { @@ -387,9 +389,36 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] { }; ``` +### Rust + +```rust +impl Solution { + pub fn next_greater_element(nums1: Vec, nums2: Vec) -> Vec { + let mut ans = vec![-1; nums1.len()]; + use std::collections::HashMap; + let mut map = HashMap::new(); + for (idx, &i) in nums1.iter().enumerate() { + map.insert(i, idx); + } + let mut stack = vec![]; + for (idx, &i) in nums2.iter().enumerate() { + while !stack.is_empty() && nums2[*stack.last().unwrap()] < i { + let pos = stack.pop().unwrap(); + if let Some(&jdx) = map.get(&nums2[pos]) { + ans[jdx] = i; + } + } + stack.push(idx); + } + ans + } +} +``` +

+ diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index 3fd4b3b6..d211a680 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -22,7 +22,7 @@ * -10^9 <= nums[i] <= 10^9 -# 思路 +## 思路 做本题之前建议先做[739. 每日温度](https://programmercarl.com/0739.每日温度.html) 和 [496.下一个更大元素 I](https://programmercarl.com/0496.下一个更大元素I.html)。 @@ -138,7 +138,8 @@ public: ## 其他语言版本 -Java: +### Java: + ```Java class Solution { public int[] nextGreaterElements(int[] nums) { @@ -162,7 +163,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 方法 1: class Solution: @@ -196,7 +198,8 @@ class Solution: stack.append(i) return ans ``` -Go: +### Go: + ```go func nextGreaterElements(nums []int) []int { length := len(nums) @@ -218,7 +221,7 @@ func nextGreaterElements(nums []int) []int { } ``` -JavaScript: +### JavaScript: ```JS /** @@ -242,7 +245,7 @@ var nextGreaterElements = function (nums) { return res; }; ``` -TypeScript: +### TypeScript: ```typescript function nextGreaterElements(nums: number[]): number[] { @@ -266,6 +269,25 @@ function nextGreaterElements(nums: number[]): number[] { }; ``` +### Rust: + +```rust +impl Solution { + pub fn next_greater_elements(nums: Vec) -> Vec { + let mut ans = vec![-1; nums.len() * 2]; + let mut stack = vec![]; + let double = nums.repeat(2); + for (idx, &i) in double.iter().enumerate() { + while !stack.is_empty() && double[*stack.last().unwrap()] < i { + let pos = stack.pop().unwrap(); + ans[pos] = i; + } + stack.push(idx); + } + ans.into_iter().take(nums.len()).collect() + } +} +```

diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index 7ace0723..0c073db5 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -32,9 +32,9 @@ F(n) = F(n - 1) + F(n - 2),其中 n > 1 * 0 <= n <= 30 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[手把手带你入门动态规划 | leetcode:509.斐波那契数](https://www.bilibili.com/video/BV1f5411K7mo),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[手把手带你入门动态规划 | leetcode:509.斐波那契数](https://www.bilibili.com/video/BV1f5411K7mo),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -278,7 +278,7 @@ class Solution: return self.fib(n - 1) + self.fib(n - 2) ``` -### Go: +### Go ```Go func fib(n int) int { if n < 2 { @@ -479,3 +479,4 @@ public class Solution + diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md index fcdd57b0..80927583 100644 --- a/problems/0516.最长回文子序列.md +++ b/problems/0516.最长回文子序列.md @@ -152,8 +152,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```java public class Solution { @@ -175,8 +174,7 @@ public class Solution { } ``` - -Python: +### Python: ```python class Solution: @@ -193,7 +191,7 @@ class Solution: return dp[0][-1] ``` -Go: +### Go: ```Go func longestPalindromeSubseq(s string) int { @@ -222,7 +220,7 @@ func longestPalindromeSubseq(s string) int { } ``` -Javascript: +### Javascript: ```javascript const longestPalindromeSubseq = (s) => { @@ -247,7 +245,7 @@ const longestPalindromeSubseq = (s) => { }; ``` -TypeScript: +### TypeScript: ```typescript function longestPalindromeSubseq(s: string): number { @@ -281,3 +279,4 @@ function longestPalindromeSubseq(s: string): number { + diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md index 8da35114..7c9f0fce 100644 --- a/problems/0518.零钱兑换II.md +++ b/problems/0518.零钱兑换II.md @@ -41,9 +41,9 @@ * 硬币种类不超过 500 种 * 结果符合 32 位符号整数 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满背包有多少种方法?组合与排列有讲究!| LeetCode:518.零钱兑换II](https://www.bilibili.com/video/BV1KM411k75j/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有多少种方法?组合与排列有讲究!| LeetCode:518.零钱兑换II](https://www.bilibili.com/video/BV1KM411k75j/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 @@ -202,8 +202,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int change(int amount, int[] coins) { @@ -242,7 +242,7 @@ class Solution { } ``` -Python: +### Python: ```python @@ -260,7 +260,8 @@ class Solution: -Go: +### Go: + ```go func change(amount int, coins []int) int { // 定义dp数组 @@ -280,7 +281,8 @@ func change(amount int, coins []int) int { } ``` -Rust: +### Rust: + ```rust impl Solution { pub fn change(amount: i32, coins: Vec) -> i32 { @@ -297,7 +299,8 @@ impl Solution { } ``` -Javascript: +### Javascript: + ```javascript const change = (amount, coins) => { let dp = Array(amount + 1).fill(0); @@ -313,7 +316,7 @@ const change = (amount, coins) => { } ``` -TypeScript: +### TypeScript: ```typescript function change(amount: number, coins: number[]): number { @@ -328,7 +331,7 @@ function change(amount: number, coins: number[]): number { }; ``` -Scala: +### Scala: ```scala object Solution { @@ -349,4 +352,3 @@ object Solution { - diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index 48d15b0b..505a4e33 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -17,9 +17,9 @@ * 解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea" -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列,还是为了编辑距离做铺垫 | LeetCode:583.两个字符串的删除操(https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[LeetCode:583.两个字符串的删除操](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -143,8 +143,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java // dp数组中存储word1和word2最长相同子序列的长度 class Solution { @@ -215,8 +215,8 @@ class Solution { } ``` +### Python: -Python: ```python class Solution: def minDistance(self, word1: str, word2: str) -> int: @@ -234,7 +234,8 @@ class Solution: return dp[-1][-1] ``` -Go: +### Go: + ```go func minDistance(word1 string, word2 string) int { dp := make([][]int, len(word1)+1) @@ -267,7 +268,8 @@ func min(a, b int) int { return b } ``` -Javascript: +### Javascript: + ```javascript // 方法一 var minDistance = (word1, word2) => { @@ -309,7 +311,7 @@ var minDistance = function (word1, word2) { }; ``` -TypeScript: +### TypeScript: > dp版本一: diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index 44092aae..18839a26 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -19,7 +19,7 @@ 注意: 合并必须从两个树的根节点开始。 -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树](https://www.bilibili.com/video/BV1m14y1Y7JK),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 @@ -793,3 +793,4 @@ impl Solution { + diff --git a/problems/0647.回文子串.md b/problems/0647.回文子串.md index 52f12d9b..dba7218f 100644 --- a/problems/0647.回文子串.md +++ b/problems/0647.回文子串.md @@ -26,11 +26,13 @@ 提示:输入的字符串长度不会超过 1000 。 -## 暴力解法 +## 思路 + +### 暴力解法 两层for循环,遍历区间起始位置和终止位置,然后还需要一层遍历判断这个区间是不是回文。所以时间复杂度:O(n^3) -## 动态规划 +### 动态规划 动规五部曲: @@ -187,7 +189,7 @@ public: * 时间复杂度:O(n^2) * 空间复杂度:O(n^2) -## 双指针法 +### 双指针法 动态规划的空间复杂度是偏高的,我们再看一下双指针法。 @@ -231,7 +233,7 @@ public: ## 其他语言版本 -Java: +### Java: 动态规划: @@ -337,7 +339,7 @@ class Solution { } ``` -Python: +### Python: > 动态规划: ```python @@ -390,7 +392,8 @@ class Solution: return res ``` -Go: +### Go: + ```Go func countSubstrings(s string) int { res:=0 @@ -416,7 +419,8 @@ func countSubstrings(s string) int { } ``` -Javascript +### Javascript: + > 动态规划 ```javascript const countSubstrings = (s) => { @@ -462,7 +466,7 @@ const countSubstrings = (s) => { } ``` -TypeScript: +### TypeScript: > 动态规划: @@ -567,3 +571,4 @@ impl Solution { + diff --git a/problems/0649.Dota2参议院.md b/problems/0649.Dota2参议院.md index a1420a66..db6b43df 100644 --- a/problems/0649.Dota2参议院.md +++ b/problems/0649.Dota2参议院.md @@ -42,7 +42,7 @@ Dota2 参议院由来自两派的参议员组成。现在参议院希望对一 因此在第二轮只剩下第三个参议员拥有投票的权利,于是他可以宣布胜利。 -# 思路 +## 思路 这道题 题意太绕了,我举一个更形象的例子给大家捋顺一下。 @@ -70,7 +70,7 @@ Dota2 参议院由来自两派的参议员组成。现在参议院希望对一 如果对贪心算法理论基础还不了解的话,可以看看这篇:[关于贪心算法,你该了解这些!](https://programmercarl.com/贪心算法理论基础.html) ,相信看完之后对贪心就有基本的了解了。 -# 代码实现 +## 代码实现 实现代码,在每一轮循环的过程中,去过模拟优先消灭身后的对手,其实是比较麻烦的。 @@ -111,9 +111,9 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -145,7 +145,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -173,7 +173,7 @@ class Solution: return "Radiant" if R else "Dire" ``` -## Go +### Go ```go @@ -214,7 +214,7 @@ func predictPartyVictory(senateStr string) string { } ``` -## JavaScript +### JavaScript ```js var predictPartyVictory = function(senateStr) { @@ -244,7 +244,7 @@ var predictPartyVictory = function(senateStr) { }; ``` -## TypeScript +### TypeScript ```typescript function predictPartyVictory(senate: string): string { @@ -287,3 +287,4 @@ function predictPartyVictory(senate: string): string { + diff --git a/problems/0657.机器人能否返回原点.md b/problems/0657.机器人能否返回原点.md index ab4bf152..f0d33391 100644 --- a/problems/0657.机器人能否返回原点.md +++ b/problems/0657.机器人能否返回原点.md @@ -29,7 +29,7 @@ -# 思路 +## 思路 这道题目还是挺简单的,大家不要想复杂了,一波哈希法又一波图论算法啥的,哈哈。 @@ -64,9 +64,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java // 时间复杂度:O(n) @@ -86,7 +86,7 @@ class Solution { } ``` -## Python +### Python ```python # 时间复杂度:O(n) @@ -107,7 +107,7 @@ class Solution: return x == 0 and y == 0 ``` -## Go +### Go ```go func judgeCircle(moves string) bool { @@ -131,7 +131,7 @@ func judgeCircle(moves string) bool { } ``` -## JavaScript +### JavaScript ```js // 时间复杂度:O(n) @@ -150,7 +150,7 @@ var judgeCircle = function(moves) { ``` -## TypeScript +### TypeScript ```ts var judgeCircle = function (moves) { @@ -185,3 +185,4 @@ var judgeCircle = function (moves) { + diff --git a/problems/0673.最长递增子序列的个数.md b/problems/0673.最长递增子序列的个数.md index da80a4e0..0277f249 100644 --- a/problems/0673.最长递增子序列的个数.md +++ b/problems/0673.最长递增子序列的个数.md @@ -24,7 +24,7 @@ * 解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。 -# 思路 +## 思路 这道题可以说是 [300.最长上升子序列](https://programmercarl.com/0300.最长上升子序列.html) 的进阶版本 @@ -221,9 +221,9 @@ public: 还有O(nlog n)的解法,使用树状数组,今天有点忙就先不写了,感兴趣的同学可以自行学习一下,这里有我之前写的树状数组系列博客:https://blog.csdn.net/youngyangyang04/category_871105.html (十年前的陈年老文了) -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -257,7 +257,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -286,7 +286,7 @@ class Solution: return result; ``` -## Go +### Go ```go @@ -332,7 +332,7 @@ func findNumberOfLIS(nums []int) int { } ``` -## JavaScript +### JavaScript ```js var findNumberOfLIS = function(nums) { @@ -364,3 +364,4 @@ var findNumberOfLIS = function(nums) { + diff --git a/problems/0674.最长连续递增序列.md b/problems/0674.最长连续递增序列.md index 8cc270ec..485e321c 100644 --- a/problems/0674.最长连续递增序列.md +++ b/problems/0674.最长连续递增序列.md @@ -29,7 +29,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列问题,重点在于连续!| LeetCode:674.最长连续递增序列](https://www.bilibili.com/video/BV1bD4y1778v),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,重点在于连续!| LeetCode:674.最长连续递增序列](https://www.bilibili.com/video/BV1bD4y1778v),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -157,8 +157,7 @@ public: ## 其他语言版本 - -Java: +### Java: > 动态规划: ```java @@ -207,7 +206,7 @@ public static int findLengthOfLCIS(int[] nums) { } ``` -Python: +### Python: DP ```python @@ -261,7 +260,8 @@ class Solution: return result ``` -Go: +### Go: + > 动态规划: ```go func findLengthOfLCIS(nums []int) int { @@ -302,7 +302,9 @@ func findLengthOfLCIS(nums []int) int { } ``` -Rust: + +### Rust: +>动态规划 ```rust pub fn find_length_of_lcis(nums: Vec) -> i32 { if nums.is_empty() { @@ -320,7 +322,28 @@ pub fn find_length_of_lcis(nums: Vec) -> i32 { } ``` -Javascript: + +> 贪心 + +```rust +impl Solution { + pub fn find_length_of_lcis(nums: Vec) -> i32 { + let (mut res, mut count) = (1, 1); + for i in 1..nums.len() { + if nums[i] > nums[i - 1] { + count += 1; + res = res.max(count); + continue; + } + count = 1; + } + res + } +} +``` + + +### Javascript: > 动态规划: ```javascript @@ -363,7 +386,7 @@ const findLengthOfLCIS = (nums) => { }; ``` -TypeScript: +### TypeScript: > 动态规划: @@ -409,3 +432,4 @@ function findLengthOfLCIS(nums: number[]): number { + diff --git a/problems/0684.冗余连接.md b/problems/0684.冗余连接.md index c4d62d9b..8124cc7e 100644 --- a/problems/0684.冗余连接.md +++ b/problems/0684.冗余连接.md @@ -25,7 +25,7 @@ * edges 中无重复元素 * 给定的图是连通的  -# 思路 +## 思路 这道题目也是并查集基础题目。 @@ -150,9 +150,9 @@ public: 可以看出,主函数的代码很少,就判断一下边的两个节点在不在同一个集合就可以了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -205,7 +205,7 @@ class Solution { } ``` -## Python +### Python ```python @@ -256,7 +256,7 @@ class Solution: return [] ``` -## Go +### Go ```go @@ -312,7 +312,7 @@ func findRedundantConnection(edges [][]int) []int { } ``` -## JavaScript +### JavaScript ```js const n = 1005; @@ -365,3 +365,4 @@ var findRedundantConnection = function(edges) { + diff --git a/problems/0685.冗余连接II.md b/problems/0685.冗余连接II.md index 8c56afdc..31b2ad24 100644 --- a/problems/0685.冗余连接II.md +++ b/problems/0685.冗余连接II.md @@ -213,9 +213,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java @@ -335,7 +335,7 @@ class Solution { } ``` -## Python +### Python ```python @@ -426,7 +426,7 @@ class Solution: return self.getRemoveEdge(edges) ``` -## Go +### Go ```go @@ -527,7 +527,7 @@ func findRedundantDirectedConnection(edges [][]int) []int { ``` -## JavaScript +### JavaScript ```js const N = 1010; // 如题:二维数组大小的在3到1000范围内 @@ -623,3 +623,4 @@ var findRedundantDirectedConnection = function(edges) { + diff --git a/problems/0695.岛屿的最大面积.md b/problems/0695.岛屿的最大面积.md index 37a601bc..186f044c 100644 --- a/problems/0695.岛屿的最大面积.md +++ b/problems/0695.岛屿的最大面积.md @@ -22,7 +22,7 @@ * 输出:6 * 解释:答案不应该是 11 ,因为岛屿只能包含水平或垂直这四个方向上的 1 。 -# 思路 +## 思路 注意题目中每座岛屿只能由**水平方向和/或竖直方向上**相邻的陆地连接形成。 @@ -38,7 +38,7 @@ * [DFS理论基础](https://programmercarl.com/图论深搜理论基础.html) * [BFS理论基础](https://programmercarl.com/图论广搜理论基础.html) -## DFS +### DFS 很多同学,写dfs其实也是凭感觉来,有的时候dfs函数中写终止条件才能过,有的时候 dfs函数不写终止添加也能过! @@ -134,7 +134,7 @@ public: 以上两种写法的区别,我在题解: [DFS,BDF 你没注意的细节都给你列出来了!LeetCode:200. 岛屿数量](https://leetcode.cn/problems/number-of-islands/solution/by-carlsun-2-n72a/)做了详细介绍。 -## BFS +### BFS 关于广度优先搜索,如果大家还不了解的话,看这里:[广度优先搜索精讲](https://programmercarl.com/图论广搜理论基础.html) @@ -188,9 +188,9 @@ public: ``` -# 其它语言版本 -## Java -### DFS +## 其它语言版本 +### Java +#### DFS ```java // DFS class Solution { @@ -236,7 +236,7 @@ class Solution { ``` -### BFS +#### BFS ```java //BFS class Solution { @@ -290,7 +290,7 @@ class Solution { } } ``` -### DFS 優化(遇到島嶼後,就把他淹沒) +#### DFS 優化(遇到島嶼後,就把他淹沒) ```java //这里使用深度优先搜索 DFS 来完成本道题目。我们使用 DFS 计算一个岛屿的面积,同时维护计算过的最大的岛屿面积。同时,为了避免对岛屿重复计算,我们在 DFS 的时候对岛屿进行 “淹没” 操作,即将岛屿所占的地方置为 0。 public int maxAreaOfIsland(int[][] grid) { @@ -319,8 +319,8 @@ public int dfs(int[][] grid,int i,int j){ } ``` -## Python -### BFS +### Python +#### BFS ```python class Solution: def __init__(self): @@ -359,7 +359,7 @@ class Solution: self.count += 1 queue.append((new_x, new_y)) ``` -### DFS +#### DFS ```python class Solution: def __init__(self): @@ -394,3 +394,4 @@ class Solution: + diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md index 52abf578..ba5e538c 100644 --- a/problems/0704.二分查找.md +++ b/problems/0704.二分查找.md @@ -150,8 +150,8 @@ public: * [35.搜索插入位置](https://programmercarl.com/0035.搜索插入位置.html) * [34.在排序数组中查找元素的第一个和最后一个位置](https://programmercarl.com/0034.%E5%9C%A8%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%85%83%E7%B4%A0%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%92%8C%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E4%BD%8D%E7%BD%AE.html) -* 69.x 的平方根 -* 367.有效的完全平方数 +* [69.x 的平方根](https://leetcode.cn/problems/sqrtx/) +* [367.有效的完全平方数](https://leetcode.cn/problems/valid-perfect-square/) @@ -760,9 +760,58 @@ object Solution { } } ``` +**Dart:** + +```dart +(版本一)左闭右闭区间 +class Solution { + int search(List nums, int target) { + int left = 0; + int right = nums.length - 1; + while (left <= right) { + int middle = ((left + right)/2).truncate(); + switch (nums[middle].compareTo(target)) { + case 1: + right = middle - 1; + continue; + case -1: + left = middle + 1; + continue; + default: + return middle; + } + } + return -1; + } +} + +(版本二)左闭右开区间 +class Solution { + int search(List nums, int target) { + int left = 0; + int right = nums.length; + while (left < right) { + int middle = left + ((right - left) >> 1); + switch (nums[middle].compareTo(target)) { + case 1: + right = middle; + continue; + case -1: + left = middle + 1; + continue; + default: + return middle; + } + } + return -1; + } +} +``` +

+ diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md index 52b2be3b..db398649 100644 --- a/problems/0714.买卖股票的最佳时机含手续费.md +++ b/problems/0714.买卖股票的最佳时机含手续费.md @@ -39,7 +39,7 @@ 本题相对于[贪心算法:122.买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II.html),多添加了一个条件就是手续费。 -## 贪心算法 +### 贪心算法 在[贪心算法:122.买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II.html)中使用贪心策略不用关心具体什么时候买卖,只要收集每天的正利润,最后稳稳的就是最大利润了。 @@ -93,7 +93,7 @@ public: 大家也可以发现,情况三,那块代码是可以删掉的,我是为了让代码表达清晰,所以没有精简。 -## 动态规划 +### 动态规划 我在公众号「代码随想录」里将在下一个系列详细讲解动态规划,所以本题解先给出我的C++代码(带详细注释),感兴趣的同学可以自己先学习一下。 @@ -364,3 +364,4 @@ object Solution { + diff --git a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md index 042de947..7e8e3d7c 100644 --- a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md +++ b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md @@ -32,9 +32,9 @@ * 0 < prices[i] < 50000. * 0 <= fee < 50000. -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -97,8 +97,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java /** * 卖出时支付手续费 @@ -173,9 +173,9 @@ class Solution { } } ``` -``` +### python + -Python: ```python class Solution: def maxProfit(self, prices: List[int], fee: int) -> int: @@ -188,7 +188,8 @@ class Solution: return max(dp[-1][0], dp[-1][1]) ``` -Go: +### Go: + ```go // 买卖股票的最佳时机含手续费 动态规划 // 时间复杂度O(n) 空间复杂度O(n) @@ -211,7 +212,8 @@ func max(a, b int) int { } ``` -Javascript: +### Javascript: + ```javascript const maxProfit = (prices,fee) => { let dp = Array.from(Array(prices.length), () => Array(2).fill(0)); @@ -224,7 +226,7 @@ const maxProfit = (prices,fee) => { } ``` -TypeScript: +### TypeScript: ```typescript function maxProfit(prices: number[], fee: number): number { @@ -245,8 +247,9 @@ function maxProfit(prices: number[], fee: number): number { }; ``` -Rust: +### Rust: **贪心** + ```Rust impl Solution { pub fn max_profit(prices: Vec, fee: i32) -> i32 { diff --git a/problems/0718.最长重复子数组.md b/problems/0718.最长重复子数组.md index 82bf4f59..18cc0240 100644 --- a/problems/0718.最长重复子数组.md +++ b/problems/0718.最长重复子数组.md @@ -25,9 +25,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列问题,想清楚DP数组的定义 | LeetCode:718.最长重复子数组](https://www.bilibili.com/video/BV178411H7hV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - - +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,想清楚DP数组的定义 | LeetCode:718.最长重复子数组](https://www.bilibili.com/video/BV178411H7hV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -126,7 +124,7 @@ public: * 时间复杂度:O(n × m),n 为A长度,m为B长度 * 空间复杂度:O(n × m) -## 滚动数组 +### 滚动数组 在如下图中: @@ -257,8 +255,8 @@ class Solution { ## 其他语言版本 +### Java: -Java: ```java // 版本一 class Solution { @@ -300,7 +298,7 @@ class Solution { } ``` -Python: +### Python: 2维DP ```python @@ -395,7 +393,8 @@ class Solution: ``` -Go: +### Go: + ```Go func findLength(A []int, B []int) int { m, n := len(A), len(B) @@ -442,7 +441,7 @@ func max(a, b int) int { } ``` -JavaScript: +### JavaScript: > 动态规划 @@ -489,7 +488,7 @@ const findLength = (nums1, nums2) => { } ``` -TypeScript: +### TypeScript: > 动态规划: @@ -544,3 +543,4 @@ function findLength(nums1: number[], nums2: number[]): number { + diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md index d88ebbb0..c2215cf6 100644 --- a/problems/0738.单调递增的数字.md +++ b/problems/0738.单调递增的数字.md @@ -26,12 +26,14 @@ 说明: N 是在 [0, 10^9] 范围内的一个整数。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,思路不难想,但代码不好写!LeetCode:738.单调自增的数字](https://www.bilibili.com/video/BV1Kv4y1x7tP),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,思路不难想,但代码不好写!LeetCode:738.单调自增的数字](https://www.bilibili.com/video/BV1Kv4y1x7tP),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + +## 思路 -## 暴力解法 +### 暴力解法 题意很简单,那么首先想的就是暴力解法了,来我替大家暴力一波,结果自然是超时! @@ -62,7 +64,7 @@ public: * 时间复杂度:O(n × m) m为n的数字长度 * 空间复杂度:O(1) -## 贪心算法 +### 贪心算法 题目要求小于等于N的最大单调递增的整数,那么拿一个两位的数字来举例。 @@ -120,7 +122,7 @@ public: ## 其他语言版本 -### Java: +### Java ```java 版本1 class Solution { @@ -163,7 +165,7 @@ class Solution { ``` -### Python: +### Python 暴力 ```python class Solution: @@ -395,3 +397,4 @@ impl Solution { + diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index 749dc972..fc1a8063 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -211,8 +211,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```java class Solution { @@ -270,7 +269,8 @@ class Solution { } ``` -Python: +### Python: + > 未精简版本 ```python @@ -307,7 +307,7 @@ class Solution: return answer ``` -Go: +### Go: > 暴力法 @@ -384,7 +384,7 @@ func dailyTemperatures(num []int) []int { } ``` -JavaScript: +### JavaScript: ```javascript // 版本一 @@ -429,7 +429,7 @@ var dailyTemperatures = function(temperatures) { }; ``` -TypeScript: +### TypeScript: > 精简版: @@ -455,10 +455,31 @@ function dailyTemperatures(temperatures: number[]): number[] { }; ``` +### Rust: +```rust +impl Solution { + /// 单调栈的本质是以空间换时间,记录之前已访问过的非递增子序列下标 + pub fn daily_temperatures(temperatures: Vec) -> Vec { + let mut res = vec![0; temperatures.len()]; + let mut stack = vec![]; + for (idx, &value) in temperatures.iter().enumerate() { + while !stack.is_empty() && temperatures[*stack.last().unwrap()] < value { + // 弹出,并计算res中对应位置的值 + let i = stack.pop().unwrap(); + res[i] = (idx - i) as i32; + } + // 入栈 + stack.push(idx) + } + res + } +} +```

+ diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index 9eaaa4e9..6fb518c3 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -37,9 +37,9 @@ * cost[i] 将会是一个整型数据,范围为 [0, 999] 。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划开更了!| LeetCode:746. 使用最小花费爬楼梯](https://www.bilibili.com/video/BV16G411c7yZ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[动态规划开更了!| LeetCode:746. 使用最小花费爬楼梯](https://www.bilibili.com/video/BV16G411c7yZ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ----------- @@ -523,3 +523,4 @@ public class Solution + diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md index 158f76d6..41314456 100644 --- a/problems/0763.划分字母区间.md +++ b/problems/0763.划分字母区间.md @@ -24,9 +24,9 @@ * S的长度在[1, 500]之间。 * S只包含小写字母 'a' 到 'z' 。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,寻找最远的出现位置! LeetCode:763.划分字母区间](https://www.bilibili.com/video/BV18G4y1K7d5),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,寻找最远的出现位置! LeetCode:763.划分字母区间](https://www.bilibili.com/video/BV18G4y1K7d5),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -409,3 +409,4 @@ impl Solution { + diff --git a/problems/0797.所有可能的路径.md b/problems/0797.所有可能的路径.md index 2ea4ae47..ec8288c6 100644 --- a/problems/0797.所有可能的路径.md +++ b/problems/0797.所有可能的路径.md @@ -149,7 +149,7 @@ public: ``` -# 总结 +## 总结 本题是比较基础的深度优先搜索模板题,这种有向图路径问题,最合适使用深搜,当然本题也可以使用广搜,但广搜相对来说就麻烦了一些,需要记录一下路径。 @@ -159,7 +159,7 @@ public: ## 其他语言版本 -Java +### Java ```Java // 深度优先遍历 @@ -190,7 +190,8 @@ class Solution { } ``` -Python +### Python + ```python class Solution: def __init__(self): @@ -216,9 +217,9 @@ class Solution: self.path.pop() # 回溯 ``` -### Go + +

- diff --git a/problems/0827.最大人工岛.md b/problems/0827.最大人工岛.md index 9112fabd..d7879825 100644 --- a/problems/0827.最大人工岛.md +++ b/problems/0827.最大人工岛.md @@ -29,7 +29,7 @@ * 输出: 4 * 解释: 没有0可以让我们变成1,面积依然为 4。 -# 思路 +## 思路 本题的一个暴力想法,应该是遍历地图尝试 将每一个 0 改成1,然后去搜索地图中的最大的岛屿面积。 @@ -219,9 +219,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java class Solution { @@ -286,4 +286,3 @@ class Solution { - diff --git a/problems/0844.比较含退格的字符串.md b/problems/0844.比较含退格的字符串.md index ac56f6f9..c7f52202 100644 --- a/problems/0844.比较含退格的字符串.md +++ b/problems/0844.比较含退格的字符串.md @@ -38,7 +38,7 @@ 本文将给出 空间复杂度O(n)的栈模拟方法 以及空间复杂度是O(1)的双指针方法。 -## 普通方法(使用栈的思路) +### 普通方法(使用栈的思路) 这道题目一看就是要使用栈的节奏,这种匹配(消除)问题也是栈的擅长所在,跟着一起刷题的同学应该知道,在[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html),我就已经提过了一次使用栈来做类似的事情了。 @@ -100,7 +100,7 @@ public: * 时间复杂度:O(n + m) * 空间复杂度:O(n + m) -## 优化方法(从后向前双指针) +### 优化方法(从后向前双指针) 当然还可以有使用 O(1) 的空间复杂度来解决该问题。 @@ -289,7 +289,7 @@ class Solution { } ``` -### python +### Python ```python class Solution: @@ -591,3 +591,4 @@ impl Solution { + diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md index 5046df12..50a0c31a 100644 --- a/problems/0860.柠檬水找零.md +++ b/problems/0860.柠檬水找零.md @@ -50,9 +50,9 @@ * 0 <= bills.length <= 10000 * bills[i] 不是 5 就是 10 或是 20  -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,看上去复杂,其实逻辑都是固定的!LeetCode:860.柠檬水找零](https://www.bilibili.com/video/BV12x4y1j7DD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,看上去复杂,其实逻辑都是固定的!LeetCode:860.柠檬水找零](https://www.bilibili.com/video/BV12x4y1j7DD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -403,3 +403,4 @@ object Solution { + diff --git a/problems/0922.按奇偶排序数组II.md b/problems/0922.按奇偶排序数组II.md index c4654f16..72be8fa7 100644 --- a/problems/0922.按奇偶排序数组II.md +++ b/problems/0922.按奇偶排序数组II.md @@ -147,8 +147,6 @@ class Solution { } ``` -### java - ```java //方法一:采用额外的数组空间 class Solution { @@ -384,3 +382,4 @@ function sortArrayByParityII(nums: number[]): number[] { + diff --git a/problems/0941.有效的山脉数组.md b/problems/0941.有效的山脉数组.md index f43a2108..48c29eb4 100644 --- a/problems/0941.有效的山脉数组.md +++ b/problems/0941.有效的山脉数组.md @@ -33,7 +33,7 @@ * 输出:true -# 思路 +## 思路 判断是山峰,主要就是要严格的保存左边到中间,和右边到中间是递增的。 @@ -71,9 +71,9 @@ public: 如果想系统学一学双指针的话, 可以看一下这篇[双指针法:总结篇!](https://programmercarl.com/双指针总结.html) -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -101,7 +101,7 @@ class Solution { } ``` -## Python3 +### Python3 ```python class Solution: @@ -118,7 +118,7 @@ class Solution: ``` -## Go +### Go ```go func validMountainArray(arr []int) bool { @@ -142,7 +142,7 @@ func validMountainArray(arr []int) bool { } ``` -## JavaScript +### JavaScript ```js var validMountainArray = function(arr) { @@ -157,7 +157,7 @@ var validMountainArray = function(arr) { }; ``` -## TypeScript +### TypeScript ```typescript function validMountainArray(arr: number[]): boolean { @@ -177,7 +177,7 @@ function validMountainArray(arr: number[]): boolean { }; ``` -## C# +### C# ```csharp public class Solution { @@ -201,3 +201,4 @@ public class Solution { + diff --git a/problems/0968.监控二叉树.md b/problems/0968.监控二叉树.md index e2ba8ebf..be04bd47 100644 --- a/problems/0968.监控二叉树.md +++ b/problems/0968.监控二叉树.md @@ -38,9 +38,9 @@ * 给定树的节点数的范围是 [1, 1000]。 * 每个节点的值都是 0。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,二叉树与贪心的结合,有点难...... LeetCode:968.监督二叉树](https://www.bilibili.com/video/BV1SA411U75i),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,二叉树与贪心的结合,有点难...... LeetCode:968.监督二叉树](https://www.bilibili.com/video/BV1SA411U75i),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -732,3 +732,4 @@ impl Solution { + diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index a53148b3..1138d7fc 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -30,7 +30,7 @@ words[i] 由小写英文字母组成 -# 思路 +## 思路 这道题意一起就有点绕,不是那么容易懂,其实就是26个小写字符中有字符 在所有字符串里都出现的话,就输出,重复的也算。 @@ -140,7 +140,7 @@ public: ## 其他语言版本 -Java: +### Java: ```Java class Solution { @@ -174,7 +174,8 @@ class Solution { } } ``` -Python +### Python + ```python class Solution: def commonChars(self, words: List[str]) -> List[str]: @@ -218,7 +219,8 @@ class Solution: return l ``` -javaScript +### JavaScript + ```js var commonChars = function (words) { let res = [] @@ -285,7 +287,8 @@ var commonChars = function(words) { } ``` -TypeScript +### TypeScript + ```ts console.time("test") let str: string = "" @@ -321,7 +324,8 @@ TypeScript return str.split("") ``` -GO +### GO + ```golang func commonChars(words []string) []string { length:=len(words) @@ -357,7 +361,8 @@ func min(a,b int)int{ } ``` -Swift: +### Swift: + ```swift func commonChars(_ words: [String]) -> [String] { var res = [String]() @@ -397,7 +402,8 @@ func commonChars(_ words: [String]) -> [String] { } ``` -C: +### C: + ```c //若两个哈希表定义为char数组(每个单词的最大长度不会超过100,因此可以用char表示),可以提高时间和空间效率 void updateHashTable(int* hashTableOne, int* hashTableTwo) { @@ -449,7 +455,8 @@ char ** commonChars(char ** words, int wordsSize, int* returnSize){ return ret; } ``` -Scala: +### Scala: + ```scala object Solution { def commonChars(words: Array[String]): List[String] = { @@ -483,7 +490,7 @@ object Solution { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -522,3 +529,4 @@ impl Solution { + diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index 4cf69d6f..bed11c7a 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -34,9 +34,9 @@ * 1 <= K <= 10000 * -100 <= A[i] <= 100 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,这不就是常识?还能叫贪心?LeetCode:1005.K次取反后最大化的数组和](https://www.bilibili.com/video/BV138411G7LY),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,这不就是常识?还能叫贪心?LeetCode:1005.K次取反后最大化的数组和](https://www.bilibili.com/video/BV138411G7LY),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git a/problems/1035.不相交的线.md b/problems/1035.不相交的线.md index 7142d75c..74e94c84 100644 --- a/problems/1035.不相交的线.md +++ b/problems/1035.不相交的线.md @@ -19,7 +19,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列问题,换汤不换药 | LeetCode:1035.不相交的线](https://www.bilibili.com/video/BV1h84y1x7MP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,换汤不换药 | LeetCode:1035.不相交的线](https://www.bilibili.com/video/BV1h84y1x7MP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -82,8 +82,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public int maxUncrossedLines(int[] nums1, int[] nums2) { @@ -106,7 +106,8 @@ Java: } ``` -Python: +### Python: + ```python class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: @@ -120,8 +121,7 @@ class Solution: return dp[-1][-1] ``` - -Golang: +### Go: ```go func maxUncrossedLines(A []int, B []int) int { @@ -152,7 +152,7 @@ func max(a, b int) int { } ``` -Rust: +### Rust: ```rust pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 { @@ -173,7 +173,7 @@ pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 { } ``` -JavaScript: +### JavaScript: ```javascript const maxUncrossedLines = (nums1, nums2) => { @@ -196,7 +196,7 @@ const maxUncrossedLines = (nums1, nums2) => { }; ``` -TypeScript: +### TypeScript: ```typescript function maxUncrossedLines(nums1: number[], nums2: number[]): number { @@ -224,3 +224,4 @@ function maxUncrossedLines(nums1: number[], nums2: number[]): number { + diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index 932029ab..cc661317 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -35,9 +35,9 @@ * 1 <= stones.length <= 30 * 1 <= stones[i] <= 1000 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[这个背包最多能装多少?LeetCode:1049.最后一块石头的重量II](https://www.bilibili.com/video/BV14M411C7oV/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[这个背包最多能装多少?LeetCode:1049.最后一块石头的重量II](https://www.bilibili.com/video/BV14M411C7oV/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -341,7 +341,7 @@ func max(a, b int) int { } ``` -### JavaScript +### JavaScript: ```javascript /** @@ -364,7 +364,7 @@ var lastStoneWeightII = function (stones) { }; ``` -### C +### C: ```c #define MAX(a, b) (((a) > (b)) ? (a) : (b)) @@ -413,7 +413,7 @@ function lastStoneWeightII(stones: number[]): number { }; ``` -### Scala +### Scala: 滚动数组: ```scala @@ -455,7 +455,7 @@ object Solution { } ``` -### Rust +### Rust: ```rust impl Solution { @@ -477,3 +477,4 @@ impl Solution { + diff --git a/problems/1143.最长公共子序列.md b/problems/1143.最长公共子序列.md index 68269b87..260b085e 100644 --- a/problems/1143.最长公共子序列.md +++ b/problems/1143.最长公共子序列.md @@ -39,7 +39,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划子序列问题经典题目 | LeetCode:1143.最长公共子序列](https://www.bilibili.com/video/BV1ye4y1L7CQ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划子序列问题经典题目 | LeetCode:1143.最长公共子序列](https://www.bilibili.com/video/BV1ye4y1L7CQ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -136,7 +136,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java /* @@ -207,8 +207,9 @@ class Solution { } ``` -Python: +### Python: 2维DP + ```python class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @@ -252,7 +253,8 @@ class Solution: ``` -Go: +### Go: + ```Go func longestCommonSubsequence(text1 string, text2 string) int { t1 := len(text1) @@ -283,7 +285,8 @@ func max(a,b int)int { ``` -Javascript: +### JavaScript: + ```javascript const longestCommonSubsequence = (text1, text2) => { let dp = Array.from(Array(text1.length+1), () => Array(text2.length+1).fill(0)); @@ -302,7 +305,7 @@ const longestCommonSubsequence = (text1, text2) => { }; ``` -TypeScript: +### TypeScript: ```typescript function longestCommonSubsequence(text1: string, text2: string): number { @@ -326,7 +329,8 @@ function longestCommonSubsequence(text1: string, text2: string): number { }; ``` -Rust: +### Rust: + ```rust pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { let (n, m) = (text1.len(), text2.len()); @@ -353,3 +357,4 @@ pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { + diff --git a/problems/1207.独一无二的出现次数.md b/problems/1207.独一无二的出现次数.md index 1a7a0019..83ebbcb7 100644 --- a/problems/1207.独一无二的出现次数.md +++ b/problems/1207.独一无二的出现次数.md @@ -31,7 +31,7 @@ * -1000 <= arr[i] <= 1000 -# 思路 +## 思路 这道题目数组在是哈希法中的经典应用,如果对数组在哈希法中的使用还不熟悉的同学可以看这两篇:[数组在哈希法中的应用](https://programmercarl.com/0242.有效的字母异位词.html)和[哈希法:383. 赎金信](https://programmercarl.com/0383.赎金信.html) @@ -71,9 +71,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java class Solution { @@ -97,7 +97,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 方法 1: 数组在哈西法的应用 class Solution: @@ -133,10 +134,8 @@ class Solution: ``` +### JavaScript: -Go: - -JavaScript: ``` javascript // 方法一:使用数组记录元素出现次数 var uniqueOccurrences = function(arr) { @@ -171,7 +170,7 @@ var uniqueOccurrences = function(arr) { }; ``` -TypeScript: +### TypeScript: > 借用数组: @@ -209,3 +208,4 @@ function uniqueOccurrences(arr: number[]): boolean { + diff --git a/problems/1356.根据数字二进制下1的数目排序.md b/problems/1356.根据数字二进制下1的数目排序.md index b898b7f2..cc7a7007 100644 --- a/problems/1356.根据数字二进制下1的数目排序.md +++ b/problems/1356.根据数字二进制下1的数目排序.md @@ -46,7 +46,7 @@ -# 思路 +## 思路 这道题其实是考察如何计算一个数的二进制中1的数量。 @@ -87,7 +87,7 @@ int bitCount(int n) { 下面我就使用方法二,来做这道题目: -## C++代码 + ```C++ class Solution { @@ -116,9 +116,9 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -151,7 +151,7 @@ class Solution { -## Python +### Python ```python class Solution: @@ -167,7 +167,7 @@ class Solution: return count ``` -## Go +### Go ```go func sortByBits(arr []int) []int { @@ -205,7 +205,7 @@ func bitCount(n int) int { } ``` -## JavaScript +### JavaScript ```js var sortByBits = function(arr) { @@ -227,3 +227,4 @@ var sortByBits = function(arr) { + diff --git a/problems/1365.有多少小于当前数字的数字.md b/problems/1365.有多少小于当前数字的数字.md index 7c268769..c706ba21 100644 --- a/problems/1365.有多少小于当前数字的数字.md +++ b/problems/1365.有多少小于当前数字的数字.md @@ -39,7 +39,7 @@ * 2 <= nums.length <= 500 * 0 <= nums[i] <= 100 -# 思路 +## 思路 两层for循环暴力查找,时间复杂度明显为$O(n^2)$。 @@ -113,9 +113,9 @@ public: 可以排序之后加哈希,时间复杂度为$O(n\log n)$ -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```Java public int[] smallerNumbersThanCurrent(int[] nums) { @@ -136,7 +136,8 @@ public int[] smallerNumbersThanCurrent(int[] nums) { } ``` -Python: +### Python: + ```python class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: @@ -151,7 +152,8 @@ class Solution: return res ``` -Go: +### Go: + ```go func smallerNumbersThanCurrent(nums []int) []int { // map,key[数组中出现的数] value[比这个数小的个数] @@ -180,7 +182,8 @@ func smallerNumbersThanCurrent(nums []int) []int { } ``` -JavaScript: +### JavaScript: + ```javascript // 方法一:使用哈希表记录位置 var smallerNumbersThanCurrent = function(nums) { @@ -217,7 +220,7 @@ var smallerNumbersThanCurrent = function(nums) { }; ``` -TypeScript: +### TypeScript: > 暴力法: diff --git a/problems/1382.将二叉搜索树变平衡.md b/problems/1382.将二叉搜索树变平衡.md index 0f81745c..57e56b8f 100644 --- a/problems/1382.将二叉搜索树变平衡.md +++ b/problems/1382.将二叉搜索树变平衡.md @@ -28,7 +28,7 @@ * 树节点的数目在 1 到 10^4 之间。 * 树节点的值互不相同,且在 1 到 10^5 之间。 -# 思路 +## 思路 这道题目,可以中序遍历把二叉树转变为有序数组,然后在根据有序数组构造平衡二叉搜索树。 @@ -71,9 +71,10 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 + +### Java: -Java: ```java class Solution { ArrayList res = new ArrayList(); @@ -99,7 +100,8 @@ class Solution { } } ``` -Python: +### Python: + ```python class Solution: def balanceBST(self, root: TreeNode) -> TreeNode: @@ -121,7 +123,7 @@ class Solution: traversal(root) return getTree(res, 0, len(res) - 1) ``` -Go: +### Go: ```go /** @@ -163,7 +165,8 @@ func balanceBST(root *TreeNode) *TreeNode { ``` -JavaScript: +### JavaScript: + ```javascript var balanceBST = function(root) { const res = []; @@ -188,7 +191,7 @@ var balanceBST = function(root) { }; ``` -TypeScript: +### TypeScript: ```typescript function balanceBST(root: TreeNode | null): TreeNode | null { @@ -218,3 +221,4 @@ function buildTree(arr: number[], left: number, right: number): TreeNode | null + diff --git a/problems/1791.找出星型图的中心节点.md b/problems/1791.找出星型图的中心节点.md index d44c1476..9bcc7ef9 100644 --- a/problems/1791.找出星型图的中心节点.md +++ b/problems/1791.找出星型图的中心节点.md @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 1791.找出星型图的中心节点 [题目链接](https://leetcode.cn/problems/find-center-of-star-graph/) @@ -55,7 +56,7 @@ public: return -1; } }; -``` +``` 以上代码中没有使用 unordered_map,因为遍历的时候,开辟新空间会浪费时间,而采用 vector,这是 空间换时间的一种策略。 diff --git a/problems/1971.寻找图中是否存在路径.md b/problems/1971.寻找图中是否存在路径.md index 5f1d8943..29e50ab8 100644 --- a/problems/1971.寻找图中是否存在路径.md +++ b/problems/1971.寻找图中是否存在路径.md @@ -138,3 +138,4 @@ public: + diff --git a/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md b/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md index a488c0ba..8be48f38 100644 --- a/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md +++ b/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md @@ -13,7 +13,7 @@ 计算机究竟1s可以执行多少次操作呢? 接下来探讨一下这个问题。 -# 超时是怎么回事 +## 超时是怎么回事 ![程序超时](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729112716117.png) @@ -25,7 +25,7 @@ 如果n的规模已经足够让O(n)的算法运行时间超过了1s,就应该考虑log(n)的解法了。 -# 从硬件配置看计算机的性能 +## 从硬件配置看计算机的性能 计算机的运算速度主要看CPU的配置,以2015年MacPro为例,CPU配置:2.7 GHz Dual-Core Intel Core i5 。 @@ -44,7 +44,7 @@ 所以我们的程序在计算机上究竟1s真正能执行多少次操作呢? -# 做个测试实验 +## 做个测试实验 在写测试程序测1s内处理多大数量级数据的时候,有三点需要注意: @@ -155,7 +155,7 @@ O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符 至于O(log n)和O(n^3) 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。 -# 完整测试代码 +## 完整测试代码 ```CPP #include @@ -212,7 +212,7 @@ int main() { ``` -# 总结 +## 总结 本文详细分析了在leetcode上做题程序为什么会有超时,以及从硬件配置上大体知道CPU的执行速度,然后亲自做一个实验来看看O(n)的算法,跑一秒钟,这个n究竟是做大,最后给出不同时间复杂度,一秒内可以运算出来的n的大小。 @@ -220,17 +220,6 @@ int main() { 这样,大家应该对程序超时时候的数据规模有一个整体的认识了。 -## 其他语言版本 - - -Java: - - -Python: - - -Go: - @@ -238,3 +227,4 @@ Go: + diff --git a/problems/qita/join.md b/problems/qita/join.md new file mode 100644 index 00000000..3b3e2d40 --- /dev/null +++ b/problems/qita/join.md @@ -0,0 +1,249 @@ + + +# 如何在Github上提交PR(pull request) + + +* 如何提交代码 +* 合入不规范 + * 提交信息不规范 + * Markdown 代码格式 + * pull request里的commit数量 + * 代码注释 + * 说明具体是哪种方法 + * 代码规范 + * 代码逻辑 + * 处理冲突 + +以下在 [https://github.com/youngyangyang04/leetcode-master](https://github.com/youngyangyang04/leetcode-master) 上提交pr为为例 + +## 如何合入代码 + +首先来说一说如何合入代码,不少录友还不太会使用github,所以这里也做一下科普。 + +我特意申请一个新的Github账号,给大家做一个示范。 + +需要强调一下,一个commit就只更新一道题目,不要很多题目一起放在一个commit里,那样就很乱。 + +首先LeetCode-Master每天都有更新,如何保持你fork到自己的仓库是最新的版本呢。 + +点击这里Fetch upstream。 + +
+ +点击之后,这里就会显示最新的信息了 +
+ +注意这时是你的远端仓库为最新版本,本地还不是最新的,本地要git pull一下。 + +基于最新的版本,大家在去提交代码。 + +如何提交代码呢,首先把自己的代码提交到自己的fork的远端仓库中,然后open pull request,如图: + +
+ +点击 open pull request之后,就是如下画面,一个pull request有多个commit。 + +
+ +然后就是给pull request 添加备注,pull request是对本次commit的一个总结。如果一个pull request就一个commit,那么就和commit的备注保持一次。 然后点击 create pull request 就可以了 + +
+ +此时你就提交成功了,我会在项目中的pull requests 处理列表里看到你的请求。 +
+ +然后如果你发现自己的代码没有合入多半是有问题,如果有问题都有会在pull request里给出留言的, + +## 注意事项 + +### 提交信息不规范 + + +大家提交代码的时候有两个地方需要写备注,一个是commit,一个是pull request,pull request包好多个commit。 + +commit 说清楚本文件多了哪些修改,而pull request则是对本次合入的所有commit做一个总结性描述。 + +commit备注,举例:添加Rust python3,那么commit备注就是:添加0001两数之和 Rust python3 版本 + +而pull request 如果只有一个commit,那么就也是:添加0001两数之和 Rust python3 版本 + +如果是多个commit ,则把本次commit都描述一遍。 + +### Markdown 语法 + +关于 Markdown 代码格式,例如 添加C++代码,需要有代码块语法 + +\`\`\`C++ +C++代码 +\`\`\` + +例如这个commit,在添加java代码的时候,就直接添加代码 +
+ +正确的格式应该是这样: +
+ +一般发现问题,我也会在代码中给出评论: + +
+ +这样大家也可以学习一些 提交代码的规范方面的知识 + + +有的录友 是添加的代码块语法,但没有标记是哪种语言,这样的话 代码就不会针对某种语言高亮显示了,也比较影响阅读,例如: + +
+ +提交python代码的话,要注释好,是python2还是python3 + +例如这样: + +
+ +当然python2的话,只这么写就行 + +\`\`\`python +python代码 +\`\`\` + +### pull request里的commit数量 + + +有的录友是一个pull request 里有很多commit (一个commit是一道题目的代码)。 + +有的录友是一个pull request 里有有一个commit。 + +
+ +其实如果大家是平时一天写了两三道题目的话,那么分三个commit,一个pull request提交上来就行。 + +一个pull request 一个commit也可以,这样大家就会麻烦一点。 + +但注意一个pull request也不要放太多的commit,一旦有一道题目代码不合格,我没有合入,就这个pull request里影响其他所有代码的合入了。 + +### 代码注释 + +提交的代码最好要有注释,这样也方便读者理解。 + +例如这位录友,在提交Java代码的时候,按照题解的意思对Java版本的代码进行的注释,这就很棒👍 + +
+ +
+ +当然如果大家感觉 已有的代码 不符合以上要求的话,例如 代码思路不够清晰不够规范,注释不够友好,依然欢迎提交优化代码,要记得详细注释哦。 + +
+ +### 说明具体是哪种方法 + +有的题解有两种甚至三四种解法,在添加代码的时候,注释上也清楚具体是哪一种方法的版本。 + +下面这位录友做的就很好 + +
+ + +
+ +有的题解,是一起给出了多道题目的讲解,例如项目中0102.二叉树的层序遍历.md 中有八道题目,那么大家添加代码的时候 应该在代码注释上,或者 直接写上 是哪个题目的代码。 + + +### 代码规范 + + +大家提交代码要规范,当然代码可以在力扣上运行通过是最基本的。 + +虽然我主张没有绝对正确的代码风格,但既然是给LeetCode-Master提交代码,尽量遵循Google编程规范。 + +经常看我的代码的录友应该都知道,我的代码格严格按照 Google C++ 编程规范来的,这样看上去会比较整洁。 + +大家提交代码的时候遇到规范性问题,例如哪里应该由空格,哪里没有空格,可以参考我的代码来。 + +有一位录友在提交代码的时候会把之前的代码 做一下规范性的调整,这就很棒。 + +
+ +**代码规范从你我做起!** + + +### 代码逻辑 + +**提交的代码要按照题解思路来写**。 + +虽然大家自己发挥想象空间是好的,但是题解还是要一脉相承,读者看完题解,发现代码和题解不是一个思路的话,那和重新读代码有啥区别了。 + +所以和题解不是一个思路的代码,除非详细注释了自己的思路 或者 写一段自己代码的描述说明思路和优化的地方,否则我就不会通过合入了哈。 + +大家的代码 最好也将关键地方放上注释,这样有助于别人快速理解你的代码。 + + +### 处理冲突 + +在合入的过程中还要处理冲突的代码, 理解大家代码的思路,解决冲突,然后在力扣提交一下,确保是没问题。 + +例如同一道题目, 一位录友提交了, 我还没处理如何,另一位录友也对这道题也提交了代码,这样就会发生冲突 +
+ +大家提交代码的热情太高了,我有时候根本处理不过来,但我必须当天处理完,否则第二天代码冲突会越来越多。 +
+ +一天晚分别有两位录友提交了 30多道 java代码,全部冲突,解决冲突处理的我脖子疼[哭] + +那么在处理冲突的时候 保留谁的代码,删点谁的代码呢? + +我一定是看谁 代码逻辑和题解一致,代码风格好,注释友好,就保留谁的。 + +所以例如当你想提交Java代码的时候,即使发现该题解已经有Java版本了,只要你的代码写的好,一样可以提交,我评审合格一样可以合入代码库。 + + +### 不要做额外修改 + +确保这种额外文件不要提交。 + +
+ +还有添加不同方法的时候,直接用正文格式写,哪种方法就可以了,不要添加目录 ,例如这样,这样整篇文章目录结构就有影响了。 + +
+ +前面不要加 `## 前序遍历(迭代法)`,直接写`前序遍历(迭代法)`就可以了。 + +当然这里也没有给代码块标记上对应的语言,应该是 + +\`\`\` Go +Go语言代码 +\`\`\` + + +## 对代码保持敬畏 + +有的录友甚至提交的代码并不是本题的代码,虽然我是鼓励大家提交代码的,但是大家贡献代码的时候也要对 自己的代码有敬畏之心,自己的代码是要给很多读者看的。 + +* 代码运行无误 +* 写的够不够简洁 +* 注释清不清晰 +* 备注规不规范 + +这也是培养大家以后协调工作的一种能力。 + +## 优化 + +目前 leetcode-master中大部分题解已经补充了其他语言,但如果你发现了可以优化的地方,依然可以提交PR来优化。 + +甚至发现哪里有语病,也欢迎提交PR来修改,例如下面:就是把【下表】 纠正为【下标】 + +
+ +不用非要写出牛逼的代码才能提交PR,只要发现 文章中有任何问题,或者错别字,都欢迎提交PR,成为contributor。 + +
+ +## 特别注意 + +git add之前,要git diff 查看一下,本次提交所修改的代码是不是 自己修改的,是否 误删,或者误加的文件。 + +提交代码,不要使用git push -f 这种命令,要足够了解 -f 意味着什么。 + + + diff --git a/problems/qita/参与本项目.md b/problems/qita/参与本项目.md deleted file mode 100644 index 76a2c61e..00000000 --- a/problems/qita/参与本项目.md +++ /dev/null @@ -1,15 +0,0 @@ - -优化已有代码 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210821161813.png) - -**push代码之前 一定要 先pull最新代码**,否则提交的pr可能会有删除其他录友代码的操作。 - -一个pr 不要修改过多文件,因为一旦有一个 文件修改有问题,就不能合入,影响其他文件的合入了。 - -git add之前,要git diff 查看一下,本次提交所修改的代码是不是 自己修改的,是否 误删,或者误加的文件。 - -提交代码,不要使用git push -f 这种命令,要足够了解 -f 意味着什么。 - - -不用非要写出牛逼的代码才能提交PR,只要发现 文章中有任何问题,或者错别字,都欢迎提交PR,成为contributor。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210927113149.png) diff --git a/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md b/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md index b8adffc7..1982d449 100644 --- a/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md +++ b/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md @@ -163,8 +163,8 @@ else { ## 其他语言版本 +### Java: -Java: ```java class Solution { public int minDistance(String word1, String word2) { @@ -193,11 +193,6 @@ class Solution { } ``` -Python: - - -Go: - diff --git a/problems/关于时间复杂度,你不知道的都在这里!.md b/problems/关于时间复杂度,你不知道的都在这里!.md index c479dddc..95c7567c 100644 --- a/problems/关于时间复杂度,你不知道的都在这里!.md +++ b/problems/关于时间复杂度,你不知道的都在这里!.md @@ -8,6 +8,8 @@ 所以重新整理的时间复杂度文章,正式和大家见面啦! +# 时间复杂度 + ## 究竟什么是时间复杂度 **时间复杂度是一个函数,它定性描述该算法的运行时间**。 @@ -145,7 +147,7 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 **当然这不是这道题目的最优解,我仅仅是用这道题目来讲解一下时间复杂度**。 -# 总结 +## 总结 本篇讲解了什么是时间复杂度,复杂度是用来干什么,以及数据规模对时间复杂度的影响。 @@ -157,17 +159,6 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 如果感觉「代码随想录」很不错,赶快推荐给身边的朋友同学们吧,他们发现和「代码随想录」相见恨晚! -## 其他语言版本 - - -Java: - - -Python: - - -Go: - @@ -175,3 +166,4 @@ Go: + diff --git a/problems/前序/ACM模式如何构建二叉树.md b/problems/前序/ACM模式如何构建二叉树.md index b6446403..48781eda 100644 --- a/problems/前序/ACM模式如何构建二叉树.md +++ b/problems/前序/ACM模式如何构建二叉树.md @@ -1,6 +1,8 @@ # 力扣上如何自己构造二叉树输入用例? +**这里给大家推荐ACM模式练习网站**:[kamacoder.com](https://kamacoder.com),把上面的题目刷完,ACM模式就没问题了。 + 经常有录友问,二叉树的题目中输入用例在ACM模式下应该怎么构造呢? 力扣上的题目,输入用例就给了一个数组,怎么就能构造成二叉树呢? diff --git a/problems/前序/什么是核心代码模式,什么又是ACM模式?.md b/problems/前序/什么是核心代码模式,什么又是ACM模式?.md index 0b9d230f..0012f88e 100644 --- a/problems/前序/什么是核心代码模式,什么又是ACM模式?.md +++ b/problems/前序/什么是核心代码模式,什么又是ACM模式?.md @@ -5,6 +5,10 @@ 什么是ACM输入模式呢? 就是自己构造输入数据格式,把要需要处理的容器填充好,OJ不会给你任何代码,包括include哪些函数都要自己写,最后也要自己控制返回数据的格式。 + +**这里给大家推荐ACM模式练习网站**:[kamacoder.com](https://kamacoder.com),把上面的题目刷完,ACM模式就没问题了。 + + 而力扣上是核心代码模式,就是把要处理的数据都已经放入容器里,可以直接写逻辑,例如这样: ```CPP diff --git a/problems/前序/代码风格.md b/problems/前序/代码风格.md index b5083460..8fd8b40c 100644 --- a/problems/前序/代码风格.md +++ b/problems/前序/代码风格.md @@ -29,7 +29,7 @@ 这里我简单说一说规范问题。 -**权威的C++规范以Google为主**,我给大家下载了一份中文版本,在公众号「代码随想录」后台回复:googlec++编程规范,就可以领取。(涉及到微信后台的回复,没更改) +**权威的C++规范以Google为主**,我给大家下载了一份中文版本,在公众号「代码随想录」后台回复:编程规范,就可以领取。 **具体的规范要以自己团队风格为主**,融入团队才是最重要的。 diff --git a/problems/前序/深圳互联网公司总结.md b/problems/前序/深圳互联网公司总结.md index f8c07016..52a8448b 100644 --- a/problems/前序/深圳互联网公司总结.md +++ b/problems/前序/深圳互联网公司总结.md @@ -36,7 +36,7 @@ * 微众银行(总部深圳) * 招银科技(总部深圳) * 平安系列(平安科技、平安寿险、平安产险、平安金融、平安好医生等) -* Shopee(东南亚最大的电商平台,最近发展势头非常强劲) +* Shopee(21年有裁员风波) * 有赞(深圳) * 迅雷(总部深圳) * 金蝶(总部深圳) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index 008b7915..a3fb7ab3 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -142,7 +142,7 @@ class Solution { } ``` -### python: +### Python: (版本一)使用切片 ```python @@ -338,7 +338,7 @@ func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) { ``` -### PHP +### PHP: ```php function reverseLeftWords($s, $n) { @@ -418,4 +418,3 @@ impl Solution { - diff --git a/problems/动态规划-股票问题总结篇.md b/problems/动态规划-股票问题总结篇.md index 2b04b7b0..4df21fb7 100644 --- a/problems/动态规划-股票问题总结篇.md +++ b/problems/动态规划-股票问题总结篇.md @@ -471,21 +471,10 @@ public: 「代码随想录」值得推荐给身边每一位学习算法的朋友同学们,关注后都会发现相见恨晚! -## 其他语言版本 - - -Java: - - -Python: - - -Go: - -

+ diff --git a/problems/动态规划总结篇.md b/problems/动态规划总结篇.md index 8a1531f8..e28bfd04 100644 --- a/problems/动态规划总结篇.md +++ b/problems/动态规划总结篇.md @@ -40,7 +40,7 @@ 好啦,我们再一起回顾一下,动态规划专题中我们都讲了哪些内容。 -## 动划基础 +## 动态规划基础 * [关于动态规划,你该了解这些!](https://programmercarl.com/动态规划理论基础.html) * [动态规划:斐波那契数](https://programmercarl.com/0509.斐波那契数.html) diff --git a/problems/动态规划理论基础.md b/problems/动态规划理论基础.md index a99c0690..bff26d1d 100644 --- a/problems/动态规划理论基础.md +++ b/problems/动态规划理论基础.md @@ -12,7 +12,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划理论基础](https://www.bilibili.com/video/BV13Q4y197Wg),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划理论基础](https://www.bilibili.com/video/BV13Q4y197Wg),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 什么是动态规划 @@ -135,3 +135,4 @@ + diff --git a/problems/周总结/20201030回溯周末总结.md b/problems/周总结/20201030回溯周末总结.md index 61270161..7f6fd114 100644 --- a/problems/周总结/20201030回溯周末总结.md +++ b/problems/周总结/20201030回溯周末总结.md @@ -9,6 +9,8 @@ -------------------------- +# 本周小结!(回溯算法系列一) + ## 周一 本周我们正式开始了回溯算法系列,那么首先当然是概述。 diff --git a/problems/周总结/20210107动规周末总结.md b/problems/周总结/20210107动规周末总结.md index b4baa4ad..831ce348 100644 --- a/problems/周总结/20210107动规周末总结.md +++ b/problems/周总结/20210107动规周末总结.md @@ -1,3 +1,4 @@ +# 本周小结!(动态规划系列一) 这周我们正式开始动态规划的学习! diff --git a/problems/周总结/20210114动规周末总结.md b/problems/周总结/20210114动规周末总结.md index a77efa2f..71cb49a9 100644 --- a/problems/周总结/20210114动规周末总结.md +++ b/problems/周总结/20210114动规周末总结.md @@ -1,3 +1,4 @@ +# 本周小结!(动态规划系列二) ## 周一 diff --git a/problems/周总结/20210225动规周末总结.md b/problems/周总结/20210225动规周末总结.md index 8fd292ed..e74dd6be 100644 --- a/problems/周总结/20210225动规周末总结.md +++ b/problems/周总结/20210225动规周末总结.md @@ -1,3 +1,4 @@ +# 本周小结!(动态规划系列六) 本周我们主要讲解了打家劫舍系列,这个系列也是dp解决的经典问题,那么来看看我们收获了哪些呢,一起来回顾一下吧。 diff --git a/problems/周总结/20210304动规周末总结.md b/problems/周总结/20210304动规周末总结.md index b814e1b3..ec442a39 100644 --- a/problems/周总结/20210304动规周末总结.md +++ b/problems/周总结/20210304动规周末总结.md @@ -1,3 +1,4 @@ +# 本周小结!(动态规划系列七) 本周的主题就是股票系列,来一起回顾一下吧 diff --git a/problems/栈与队列总结.md b/problems/栈与队列总结.md index 31ce9554..e7f8ef86 100644 --- a/problems/栈与队列总结.md +++ b/problems/栈与队列总结.md @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 栈与队列总结篇 ## 栈与队列的理论基础 diff --git a/problems/根据身高重建队列(vector原理讲解).md b/problems/根据身高重建队列(vector原理讲解).md index 4f8cab82..4291c80c 100644 --- a/problems/根据身高重建队列(vector原理讲解).md +++ b/problems/根据身高重建队列(vector原理讲解).md @@ -165,19 +165,9 @@ public: 相信在这里学习算法的录友们,都是想在软件行业长远发展的,都是要从事编程的工作,那么一定要深耕好一门编程语言,这个非常重要! - - - - ## 其他语言版本 - -Java: - - -Python: - -Rust: +### Rust ```rust // 版本二,使用list(链表) @@ -206,8 +196,7 @@ impl Solution{ } ``` - -Go: +### Go Go中slice的`append`操作和C++中vector的扩容机制基本相同。 diff --git a/problems/算法模板.md b/problems/算法模板.md index 59de69df..21b7f93b 100644 --- a/problems/算法模板.md +++ b/problems/算法模板.md @@ -3,8 +3,11 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+# 算法模板 -## 二分查找法 +## 算法模板 + +### 二分查找法 ```CPP class Solution { @@ -29,7 +32,7 @@ public: ``` -## KMP +### KMP ```CPP void kmp(int* next, const string& s){ @@ -47,7 +50,7 @@ void kmp(int* next, const string& s){ } ``` -## 二叉树 +### 二叉树 二叉树的定义: @@ -60,7 +63,7 @@ struct TreeNode { }; ``` -### 深度优先遍历(递归) +#### 深度优先遍历(递归) 前序遍历(中左右) ```CPP @@ -90,7 +93,7 @@ void traversal(TreeNode* cur, vector& vec) { } ``` -### 深度优先遍历(迭代法) +#### 深度优先遍历(迭代法) 相关题解:[0094.二叉树的中序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0094.二叉树的中序遍历.md) @@ -170,7 +173,7 @@ vector postorderTraversal(TreeNode* root) { return result; } ``` -### 广度优先遍历(队列) +#### 广度优先遍历(队列) 相关题解:[0102.二叉树的层序遍历](https://programmercarl.com/0102.二叉树的层序遍历.html) @@ -208,7 +211,7 @@ vector> levelOrder(TreeNode* root) { * [0111.二叉树的最小深度(迭代法)](https://programmercarl.com/0111.二叉树的最小深度.html) * [0222.完全二叉树的节点个数(迭代法)](https://programmercarl.com/0222.完全二叉树的节点个数.html) -### 二叉树深度 +#### 二叉树深度 ```CPP int getDepth(TreeNode* node) { @@ -217,7 +220,7 @@ int getDepth(TreeNode* node) { } ``` -### 二叉树节点数量 +#### 二叉树节点数量 ```CPP int countNodes(TreeNode* root) { @@ -226,7 +229,7 @@ int countNodes(TreeNode* root) { } ``` -## 回溯算法 +### 回溯算法 ```CPP void backtracking(参数) { if (终止条件) { @@ -243,7 +246,7 @@ void backtracking(参数) { ``` -## 并查集 +### 并查集 ```CPP int n = 1005; // 根据题意而定 @@ -278,9 +281,9 @@ void backtracking(参数) { (持续补充ing) ## 其他语言版本 -JavaScript: +### JavaScript: -## 二分查找法 +#### 二分查找法 使用左闭右闭区间 @@ -322,7 +325,7 @@ var search = function (nums, target) { }; ``` -## KMP +#### KMP ```javascript var kmp = function (next, s) { @@ -340,9 +343,9 @@ var kmp = function (next, s) { } ``` -## 二叉树 +#### 二叉树 -### 深度优先遍历(递归) +##### 深度优先遍历(递归) 二叉树节点定义: @@ -387,7 +390,7 @@ var postorder = function (root, list) { } ``` -### 深度优先遍历(迭代) +##### 深度优先遍历(迭代) 前序遍历(中左右): @@ -447,7 +450,7 @@ var postorderTraversal = function (root) { }; ``` -### 广度优先遍历(队列) +##### 广度优先遍历(队列) ```javascript var levelOrder = function (root) { @@ -469,7 +472,7 @@ var levelOrder = function (root) { }; ``` -### 二叉树深度 +##### 二叉树深度 ```javascript var getDepth = function (node) { @@ -478,7 +481,7 @@ var getDepth = function (node) { } ``` -### 二叉树节点数量 +##### 二叉树节点数量 ```javascript var countNodes = function (root) { @@ -487,7 +490,7 @@ var countNodes = function (root) { } ``` -## 回溯算法 +#### 回溯算法 ```javascript function backtracking(参数) { @@ -505,7 +508,7 @@ function backtracking(参数) { ``` -## 并查集 +#### 并查集 ```javascript let n = 1005; // 根据题意而定 @@ -536,9 +539,9 @@ function backtracking(参数) { } ``` -TypeScript: +### TypeScript: -## 二分查找法 +#### 二分查找法 使用左闭右闭区间 @@ -580,7 +583,7 @@ var search = function (nums: number[], target: number): number { }; ``` -## KMP +#### KMP ```typescript var kmp = function (next: number[], s: number): void { @@ -598,9 +601,9 @@ var kmp = function (next: number[], s: number): void { } ``` -## 二叉树 +#### 二叉树 -### 深度优先遍历(递归) +##### 深度优先遍历(递归) 二叉树节点定义: @@ -650,7 +653,7 @@ var postorder = function (root: TreeNode | null, list: number[]): void { } ``` -### 深度优先遍历(迭代) +##### 深度优先遍历(迭代) 前序遍历(中左右): @@ -710,7 +713,7 @@ var postorderTraversal = function (root: TreeNode | null): number[] { }; ``` -### 广度优先遍历(队列) +##### 广度优先遍历(队列) ```typescript var levelOrder = function (root: TreeNode | null): number[] { @@ -732,7 +735,7 @@ var levelOrder = function (root: TreeNode | null): number[] { }; ``` -### 二叉树深度 +##### 二叉树深度 ```typescript var getDepth = function (node: TreNode | null): number { @@ -741,7 +744,7 @@ var getDepth = function (node: TreNode | null): number { } ``` -### 二叉树节点数量 +##### 二叉树节点数量 ```typescript var countNodes = function (root: TreeNode | null): number { @@ -750,7 +753,7 @@ var countNodes = function (root: TreeNode | null): number { } ``` -## 回溯算法 +#### 回溯算法 ```typescript function backtracking(参数) { @@ -768,7 +771,7 @@ function backtracking(参数) { ``` -## 并查集 +#### 并查集 ```typescript let n: number = 1005; // 根据题意而定 @@ -801,9 +804,9 @@ function backtracking(参数) { Java: +### Python: -Python: -## 二分查找法 +#### 二分查找法 ```python def binarysearch(nums, target): low = 0 @@ -823,7 +826,7 @@ def binarysearch(nums, target): return -1 ``` -## KMP +#### KMP ```python def kmp(self, a, s): diff --git a/problems/背包总结篇.md b/problems/背包总结篇.md index c4e8cd9c..9be93096 100644 --- a/problems/背包总结篇.md +++ b/problems/背包总结篇.md @@ -106,3 +106,4 @@ + diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index bd3191dc..7511ac87 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -8,8 +8,11 @@ # 动态规划:01背包理论基础 +## 算法公开课 -**《代码随想录》算法视频公开课:[带你学透0-1背包问题!](https://www.bilibili.com/video/BV1cg411g7Y6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透0-1背包问题!](https://www.bilibili.com/video/BV1cg411g7Y6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 这周我们正式开始讲解背包问题! @@ -37,7 +40,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 之前可能有些录友已经可以熟练写出背包了,但只要把这个文章仔细看完,相信你会意外收获! -## 01 背包 +### 01 背包 有n件物品和一个最多能背重量为w 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品只能用一次**,求解将哪些物品装入背包里物品价值总和最大。 @@ -67,7 +70,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 以下讲解和图示中出现的数字都是以这个例子为例。 -## 二维dp数组01背包 +### 二维dp数组01背包 依然动规五部曲分析一波。 @@ -226,9 +229,6 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括 主要就是自己没有动手推导一下dp数组的演变过程,如果推导明白了,代码写出来就算有问题,只要把dp数组打印出来,对比一下和自己推导的有什么差异,很快就可以发现问题了。 - -## 完整c++测试代码 - ```cpp void test_2_wei_bag_problem1() { vector weight = {1, 3, 4}; @@ -275,7 +275,7 @@ int main() { ## 其他语言版本 -### java +### Java ```java public class BagProblem { @@ -396,7 +396,8 @@ public class BagProblem { ``` -### python +### Python + 无参数版 ```python def test_2_wei_bag_problem1(): @@ -456,8 +457,7 @@ if __name__ == "__main__": ``` - -### go +### Go ```go func test_2_wei_bag_problem1(weight, value []int, bagweight int) int { @@ -498,7 +498,7 @@ func main() { } ``` -### javascript +### Javascript ```js function testWeightBagProblem (weight, value, size) { diff --git a/problems/背包理论基础01背包-2.md b/problems/背包理论基础01背包-2.md index d481e044..f60e261b 100644 --- a/problems/背包理论基础01背包-2.md +++ b/problems/背包理论基础01背包-2.md @@ -6,7 +6,12 @@ # 动态规划:01背包理论基础(滚动数组) -**《代码随想录》算法视频公开课:[带你学透0-1背包问题!(滚动数组)](https://www.bilibili.com/video/BV1BU4y177kY/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透0-1背包问题!(滚动数组)](https://www.bilibili.com/video/BV1BU4y177kY/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 昨天[动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html)中是用二维dp数组来讲解01背包。 @@ -29,7 +34,7 @@ 问背包能背的物品最大价值是多少? -## 一维dp数组(滚动数组) +### 一维dp数组(滚动数组) 对于背包问题其实状态都是可以压缩的。 @@ -154,8 +159,6 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 -## 一维dp01背包完整C++测试代码 - ```CPP void test_1_wei_bag_problem() { vector weight = {1, 3, 4}; @@ -318,7 +321,7 @@ func main() { } ``` -### javaScript +### JavaScript ```js @@ -459,3 +462,4 @@ fn test_wei_bag_problem2() { + diff --git a/problems/背包问题理论基础多重背包.md b/problems/背包问题理论基础多重背包.md index 1a856bf5..50c2e5bf 100644 --- a/problems/背包问题理论基础多重背包.md +++ b/problems/背包问题理论基础多重背包.md @@ -144,8 +144,7 @@ int main() { ## 其他语言版本 - -Java: +### Java: ```Java public void testMultiPack1(){ @@ -192,7 +191,7 @@ public void testMultiPack2(){ } ``` -Python: +### Python: 改变物品数量为01背包格式(无参版) ```python @@ -315,7 +314,7 @@ if __name__ == "__main__": test_multi_pack(weight, value, nums, bagWeight) ``` -Go: +### Go: ```go package theory @@ -406,7 +405,7 @@ func Test_multiplePack(t *testing.T) { PASS ``` -TypeScript: +### TypeScript: > 版本一(改变数据源): @@ -469,3 +468,4 @@ testMultiPack(); + diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index 088a3d50..ac4c4a1c 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -7,9 +7,13 @@ # 动态规划:完全背包理论基础 -**《代码随想录》算法视频公开课:[带你学透完全背包问题! ](https://www.bilibili.com/video/BV1uK411o7c9/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +## 算法公开课 -## 完全背包 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透完全背包问题! ](https://www.bilibili.com/video/BV1uK411o7c9/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 + +### 完全背包 有N件物品和一个最多能背重量为W的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品都有无限个(也就是可以放入背包多次)**,求解将哪些物品装入背包里物品价值总和最大。 @@ -113,8 +117,6 @@ for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 } ``` -## C++测试代码 - 完整的C++测试代码如下: ```CPP @@ -181,7 +183,7 @@ int main() { ## 其他语言版本 -Java: +### Java: ```java //先遍历物品,再遍历背包 @@ -221,9 +223,7 @@ private static void testCompletePackAnotherWay(){ -Python: - - +### Python: 先遍历物品,再遍历背包(无参版) ```python @@ -299,7 +299,8 @@ if __name__ == "__main__": ``` -Go: +### Go: + ```go // test_CompletePack1 先遍历物品, 在遍历背包 @@ -352,7 +353,8 @@ func main() { fmt.Println(test_CompletePack2(weight, price, 4)) } ``` -Javascript: +### Javascript: + ```Javascript // 先遍历物品,再遍历背包容量 function test_completePack1() { @@ -385,7 +387,7 @@ function test_completePack2() { } ``` -TypeScript: +### TypeScript: ```typescript // 先遍历物品,再遍历背包容量 @@ -404,7 +406,7 @@ function test_CompletePack(): void { test_CompletePack(); ``` -Scala: +### Scala: ```scala // 先遍历物品,再遍历背包容量 @@ -426,7 +428,7 @@ object Solution { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -468,3 +470,4 @@ fn test_complete_pack() { + diff --git a/problems/贪心算法总结篇.md b/problems/贪心算法总结篇.md index d52e8551..c375503d 100644 --- a/problems/贪心算法总结篇.md +++ b/problems/贪心算法总结篇.md @@ -154,3 +154,4 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是 + diff --git a/problems/链表总结篇.md b/problems/链表总结篇.md index dacd4dee..3f2f5c97 100644 --- a/problems/链表总结篇.md +++ b/problems/链表总结篇.md @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 链表总结篇