diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md
index bf1e173e..4e44d0c3 100644
--- a/problems/0001.两数之和.md
+++ b/problems/0001.两数之和.md
@@ -37,20 +37,20 @@
[242. 有效的字母异位词](https://www.programmercarl.com/0242.有效的字母异位词.html) 这道题目是用数组作为哈希表来解决哈希问题,[349. 两个数组的交集](https://www.programmercarl.com/0349.两个数组的交集.html)这道题目是通过set作为哈希表来解决哈希问题。
-首先我在强调一下 **什么时候使用哈希法**,当我们需要查询一个元素是否出现过,或者一个元素是否在集合里的时候,就要第一时间想到哈希法。
+首先我再强调一下 **什么时候使用哈希法**,当我们需要查询一个元素是否出现过,或者一个元素是否在集合里的时候,就要第一时间想到哈希法。
本题呢,我就需要一个集合来存放我们遍历过的元素,然后在遍历数组的时候去询问这个集合,某元素是否遍历过,也就是 是否出现在这个集合。
那么我们就应该想到使用哈希法了。
-因为本地,我们不仅要知道元素有没有遍历过,还要知道这个元素对应的下标,**需要使用 key value结构来存放,key来存元素,value来存下标,那么使用map正合适**。
+因为本题,我们不仅要知道元素有没有遍历过,还要知道这个元素对应的下标,**需要使用 key value结构来存放,key来存元素,value来存下标,那么使用map正合适**。
再来看一下使用数组和set来做哈希法的局限。
* 数组的大小是受限制的,而且如果元素很少,而哈希值太大会造成内存空间的浪费。
* set是一个集合,里面放的元素只能是一个key,而两数之和这道题目,不仅要判断y是否存在而且还要记录y的下标位置,因为要返回x 和 y的下标。所以set 也不能用。
-此时就要选择另一种数据结构:map ,map是一种key value的存储结构,可以用key保存数值,用value在保存数值所在的下标。
+此时就要选择另一种数据结构:map ,map是一种key value的存储结构,可以用key保存数值,用value再保存数值所在的下标。
C++中map,有三种类型:
@@ -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/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/0039.组合总和.md b/problems/0039.组合总和.md
index 2a36ce5a..37d5614e 100644
--- a/problems/0039.组合总和.md
+++ b/problems/0039.组合总和.md
@@ -73,7 +73,7 @@ candidates 中的数字可以无限制重复被选取。
如果是多个集合取组合,各个集合之间相互不影响,那么就不用startIndex,例如:[17.电话号码的字母组合](https://programmercarl.com/0017.电话号码的字母组合.html)
-**注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路,后面我再讲解排列的时候就重点介绍**。
+**注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路,后面我在讲解排列的时候会重点介绍**。
代码如下:
@@ -311,7 +311,7 @@ class Solution:
for i in range(startIndex, len(candidates)):
if total + candidates[i] > target:
- break
+ continue
total += candidates[i]
path.append(candidates[i])
self.backtracking(candidates, target, total, i, path, result)
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/0054.螺旋矩阵.md b/problems/0054.螺旋矩阵.md
index a38e8237..c62eb2b1 100644
--- a/problems/0054.螺旋矩阵.md
+++ b/problems/0054.螺旋矩阵.md
@@ -6,7 +6,7 @@
-## 54.螺旋矩阵
+# 54.螺旋矩阵
[力扣题目链接](https://leetcode.cn/problems/spiral-matrix/)
@@ -200,6 +200,58 @@ class Solution {
}
```
+### Javascript
+```
+/**
+ * @param {number[][]} matrix
+ * @return {number[]}
+ */
+var spiralOrder = function(matrix) {
+ let m = matrix.length
+ let n = matrix[0].length
+
+ let startX = startY = 0
+ let i = 0
+ let arr = new Array(m*n).fill(0)
+ let offset = 1
+ let loop = mid = Math.floor(Math.min(m,n) / 2)
+ while (loop--) {
+ let row = startX
+ let col = startY
+ // -->
+ for (; col < n + startY - offset; col++) {
+ arr[i++] = matrix[row][col]
+ }
+ // down
+ for (; row < m + startX - offset; row++) {
+ arr[i++] = matrix[row][col]
+ }
+ // <--
+ for (; col > startY; col--) {
+ arr[i++] = matrix[row][col]
+ }
+ for (; row > startX; row--) {
+ arr[i++] = matrix[row][col]
+ }
+ startX++
+ startY++
+ offset += 2
+ }
+ if (Math.min(m, n) % 2 === 1) {
+ if (n > m) {
+ for (let j = mid; j < mid + n - m + 1; j++) {
+ arr[i++] = matrix[mid][j]
+ }
+ } else {
+ for (let j = mid; j < mid + m - n + 1; j++) {
+ arr[i++] = matrix[j][mid]
+ }
+ }
+ }
+ return arr
+};
+```
+
diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index 73e9e4da..78d9385a 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -688,7 +688,7 @@ public class Solution { } ``` -### Ruby#: +### Ruby: ```ruby def generate_matrix(n) result = Array.new(n) { Array.new(n, 0) } 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/0072.编辑距离.md b/problems/0072.编辑距离.md index 1ed9a860..2b5a1925 100644 --- a/problems/0072.编辑距离.md +++ b/problems/0072.编辑距离.md @@ -403,6 +403,64 @@ int minDistance(char * word1, char * word2){ } ``` +Rust: + +```rust +impl Solution { + pub fn min_distance(word1: String, word2: String) -> i32 { + let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1]; + for i in 1..=word2.len() { + dp[0][i] = i; + } + + for (j, v) in dp.iter_mut().enumerate().skip(1) { + v[0] = j; + } + for (i, char1) in word1.chars().enumerate() { + for (j, char2) in word2.chars().enumerate() { + if char1 == char2 { + dp[i + 1][j + 1] = dp[i][j]; + continue; + } + dp[i + 1][j + 1] = dp[i][j + 1].min(dp[i + 1][j]).min(dp[i][j]) + 1; + } + } + + dp[word1.len()][word2.len()] as i32 + } +} +``` + +> 一维 dp + +```rust +impl Solution { + pub fn min_distance(word1: String, word2: String) -> i32 { + let mut dp = vec![0; word1.len() + 1]; + for (i, v) in dp.iter_mut().enumerate().skip(1) { + *v = i; + } + + for char2 in word2.chars() { + // 相当于 dp[i][0] 的初始化 + let mut pre = dp[0]; + dp[0] += 1; // j = 0, 将前 i 个字符变成空串的个数 + for (j, char1) in word1.chars().enumerate() { + let temp = dp[j + 1]; + if char1 == char2 { + dp[j + 1] = pre; + } else { + dp[j + 1] = dp[j + 1].min(dp[j]).min(pre) + 1; + } + pre = temp; + } + } + + dp[word1.len()] as i32 + } +} +``` +
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
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){
@@ -244,3 +245,4 @@ class Solution:
diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md
index 9b76229a..9fb6a6b0 100644
--- a/problems/0084.柱状图中最大的矩形.md
+++ b/problems/0084.柱状图中最大的矩形.md
@@ -725,62 +725,6 @@ impl Solution {
}
```
-Rust
-
-双指针预处理
-```rust
-
-impl Solution {
- pub fn largest_rectangle_area(v: Vec
-
diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md
index d4dc7698..e4c5c484 100644
--- a/problems/0188.买卖股票的最佳时机IV.md
+++ b/problems/0188.买卖股票的最佳时机IV.md
@@ -529,4 +529,3 @@ impl Solution {
-
diff --git a/problems/0200.岛屿数量.广搜版.md b/problems/0200.岛屿数量.广搜版.md
index c20fe4f1..5b9d90aa 100644
--- a/problems/0200.岛屿数量.广搜版.md
+++ b/problems/0200.岛屿数量.广搜版.md
@@ -197,7 +197,7 @@ class Solution {
}
```
-## 其他语言版本
+
### Python
BFS solution
```python
@@ -237,6 +237,7 @@ class Solution:
continue
q.append((next_i, next_j))
visited[next_i][next_j] = True
+
```
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+ # 283. 移动零:动态规划:一样的套路,再求一次完全平方数 [力扣题目链接](https://leetcode.cn/problems/move-zeroes/) diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md index 11cf13d9..677c72c6 100644 --- a/problems/0300.最长上升子序列.md +++ b/problems/0300.最长上升子序列.md @@ -130,18 +130,16 @@ public: class Solution { public int lengthOfLIS(int[] nums) { int[] dp = new int[nums.length]; + int res = 0; Arrays.fill(dp, 1); - for (int i = 0; i < dp.length; i++) { + for (int i = 1; i < dp.length; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = Math.max(dp[i], dp[j] + 1); } + res = Math.max(res, dp[i]); } } - int res = 0; - for (int i = 0; i < dp.length; i++) { - res = Math.max(res, dp[i]); - } return res; } } @@ -294,7 +292,7 @@ function lengthOfLIS(nums: number[]): number { ```rust pub fn length_of_lis(nums: Vec
diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md
index 794a67fa..fcdeae7b 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/) ,相信结合视频再看本篇题解,更有助于大家对本题的理解。**
## 思路
@@ -790,4 +790,3 @@ 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/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md
index 8daf5a35..26a9286d 100644
--- a/problems/0349.两个数组的交集.md
+++ b/problems/0349.两个数组的交集.md
@@ -465,6 +465,25 @@ object Solution {
}
```
+
+###Ruby
+```ruby
+def intersection(nums1, nums2)
+ hash = {}
+ result = {}
+
+ nums1.each do |num|
+ hash[num] = 1 if hash[num].nil?
+ end
+
+ nums2.each do |num|
+ #取nums1和nums2交集
+ result[num] = 1 if hash[num] != nil
+ end
+
+ return result.keys
+end
+```
## 相关题目
* [350.两个数组的交集 II](https://leetcode.cn/problems/intersection-of-two-arrays-ii/)
diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md
index c15d1ac1..3de48ce3 100644
--- a/problems/0383.赎金信.md
+++ b/problems/0383.赎金信.md
@@ -68,7 +68,7 @@ public:
### 哈希解法
-因为题目所只有小写字母,那可以采用空间换取时间的哈希策略, 用一个长度为26的数组还记录magazine里字母出现的次数。
+因为题目说只有小写字母,那可以采用空间换取时间的哈希策略,用一个长度为26的数组来记录magazine里字母出现的次数。
然后再用ransomNote去验证这个数组是否包含了ransomNote所需要的所有字母。
diff --git a/problems/0392.判断子序列.md b/problems/0392.判断子序列.md
index 6342a41f..12d3fa48 100644
--- a/problems/0392.判断子序列.md
+++ b/problems/0392.判断子序列.md
@@ -259,9 +259,49 @@ func isSubsequence(s string, t string) bool {
}
```
+Rust:
+```rust
+impl Solution {
+ pub fn is_subsequence(s: String, t: String) -> bool {
+ let mut dp = vec![vec![0; t.len() + 1]; s.len() + 1];
+ for (i, char_s) in s.chars().enumerate() {
+ for (j, char_t) in t.chars().enumerate() {
+ if char_s == char_t {
+ dp[i + 1][j + 1] = dp[i][j] + 1;
+ continue;
+ }
+ dp[i + 1][j + 1] = dp[i + 1][j]
+ }
+ }
+ dp[s.len()][t.len()] == s.len()
+ }
+}
+```
+> 滚动数组
+```rust
+impl Solution {
+ pub fn is_subsequence(s: String, t: String) -> bool {
+ let mut dp = vec![0; t.len() + 1];
+ let (s, t) = (s.as_bytes(), t.as_bytes());
+ for &byte_s in s {
+ let mut pre = 0;
+ for j in 0..t.len() {
+ let temp = dp[j + 1];
+ if byte_s == t[j] {
+ dp[j + 1] = pre + 1;
+ } else {
+ dp[j + 1] = dp[j];
+ }
+ pre = temp;
+ }
+ }
+ dp[t.len()] == s.len()
+ }
+}
+```
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-## 416. 分割等和子集 +# 416. 分割等和子集 [力扣题目链接](https://leetcode.cn/problems/partition-equal-subset-sum/) @@ -730,4 +730,3 @@ object Solution {
diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md
index 023e4d7e..d211a680 100644
--- a/problems/0503.下一个更大元素II.md
+++ b/problems/0503.下一个更大元素II.md
@@ -294,4 +294,3 @@ impl Solution {
-
diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md
index 80927583..62f4a3da 100644
--- a/problems/0516.最长回文子序列.md
+++ b/problems/0516.最长回文子序列.md
@@ -272,7 +272,26 @@ function longestPalindromeSubseq(s: string): number {
};
```
+Rust:
+```rust
+impl Solution {
+ pub fn longest_palindrome_subseq(s: String) -> i32 {
+ let mut dp = vec![vec![0; s.len()]; s.len()];
+ for i in (0..s.len()).rev() {
+ dp[i][i] = 1;
+ for j in i + 1..s.len() {
+ if s[i..=i] == s[j..=j] {
+ dp[i][j] = dp[i + 1][j - 1] + 2;
+ continue;
+ }
+ dp[i][j] = dp[i + 1][j].max(dp[i][j - 1]);
+ }
+ }
+ dp[0][s.len() - 1]
+ }
+}
+```
diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index 505a4e33..7dbb8ef5 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -370,7 +370,51 @@ function minDistance(word1: string, word2: string): number { }; ``` +Rust: +```rust +impl Solution { + pub fn min_distance(word1: String, word2: String) -> i32 { + let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1]; + for i in 0..word1.len() { + dp[i + 1][0] = i + 1; + } + for j in 0..word2.len() { + dp[0][j + 1] = j + 1; + } + for (i, char1) in word1.chars().enumerate() { + for (j, char2) in word2.chars().enumerate() { + if char1 == char2 { + dp[i + 1][j + 1] = dp[i][j]; + continue; + } + dp[i + 1][j + 1] = dp[i][j + 1].min(dp[i + 1][j]) + 1; + } + } + dp[word1.len()][word2.len()] as i32 + } +} +``` + +> 版本 2 + +```rust +impl Solution { + pub fn min_distance(word1: String, word2: String) -> i32 { + let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1]; + for (i, char1) in word1.chars().enumerate() { + for (j, char2) in word2.chars().enumerate() { + if char1 == char2 { + dp[i + 1][j + 1] = dp[i][j] + 1; + continue; + } + dp[i + 1][j + 1] = dp[i][j + 1].max(dp[i + 1][j]); + } + } + (word1.len() + word2.len() - 2 * dp[word1.len()][word2.len()]) as i32 + } +} +```
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 fdf83736..dba7218f 100644
--- a/problems/0647.回文子串.md
+++ b/problems/0647.回文子串.md
@@ -521,8 +521,51 @@ function expandRange(s: string, left: number, right: number): number {
}
```
+Rust:
+```rust
+impl Solution {
+ pub fn count_substrings(s: String) -> i32 {
+ let mut dp = vec![vec![false; s.len()]; s.len()];
+ let mut res = 0;
+ for i in (0..s.len()).rev() {
+ for j in i..s.len() {
+ if s[i..=i] == s[j..=j] && (j - i <= 1 || dp[i + 1][j - 1]) {
+ dp[i][j] = true;
+ res += 1;
+ }
+ }
+ }
+ res
+ }
+}
+```
+
+> 双指针
+
+```rust
+impl Solution {
+ pub fn count_substrings(s: String) -> i32 {
+ let mut res = 0;
+ for i in 0..s.len() {
+ res += Self::extend(&s, i, i, s.len());
+ res += Self::extend(&s, i, i + 1, s.len());
+ }
+ res
+ }
+
+ fn extend(s: &str, mut i: usize, mut j: usize, len: usize) -> i32 {
+ let mut res = 0;
+ while i < len && j < len && s[i..=i] == s[j..=j] {
+ res += 1;
+ i = i.wrapping_sub(1);
+ j += 1;
+ }
+ res
+ }
+}
+```
diff --git a/problems/0674.最长连续递增序列.md b/problems/0674.最长连续递增序列.md
index 0c5e64b3..485e321c 100644
--- a/problems/0674.最长连续递增序列.md
+++ b/problems/0674.最长连续递增序列.md
@@ -302,8 +302,9 @@ func findLengthOfLCIS(nums []int) int {
}
```
-### Rust:
+### Rust:
+>动态规划
```rust
pub fn find_length_of_lcis(nums: Vec
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益! 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+
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
+
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/0718.最长重复子数组.md b/problems/0718.最长重复子数组.md
index 18cc0240..272cf2b2 100644
--- a/problems/0718.最长重复子数组.md
+++ b/problems/0718.最长重复子数组.md
@@ -536,6 +536,29 @@ function findLength(nums1: number[], nums2: number[]): number {
};
```
+Rust:
+
+> 滚动数组
+
+```rust
+impl Solution {
+ pub fn find_length(nums1: Vec
-
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/1002.查找常用字符.md b/problems/1002.查找常用字符.md
index 1138d7fc..57fa7dae 100644
--- a/problems/1002.查找常用字符.md
+++ b/problems/1002.查找常用字符.md
@@ -525,6 +525,54 @@ impl Solution {
}
```
+Ruby:
+```ruby
+def common_chars(words)
+ result = []
+ #统计所有字符串里字符出现的最小频率
+ hash = {}
+ #初始化标识
+ is_first = true
+
+ words.each do |word|
+ #记录共同字符
+ chars = []
+ word.split('').each do |chr|
+ #第一个字符串初始化
+ if is_first
+ chars << chr
+ else
+ #字母之前出现过的最小次数
+ if hash[chr] != nil && hash[chr] > 0
+ hash[chr] -= 1
+ chars << chr
+ end
+ end
+ end
+
+ is_first = false
+ #清除hash,更新字符最小频率
+ hash.clear
+ chars.each do |chr|
+ if hash[chr] != nil
+ hash[chr] += 1
+ else
+ hash[chr] = 1
+ end
+ end
+ end
+
+ #字符最小频率hash转换为字符数组
+ hash.keys.each do |key|
+ for i in 0..hash[key] - 1
+ result << key
+ end
+ end
+
+ return result
+end
+```
+
diff --git a/problems/1035.不相交的线.md b/problems/1035.不相交的线.md
index 74e94c84..ff9e5dc4 100644
--- a/problems/1035.不相交的线.md
+++ b/problems/1035.不相交的线.md
@@ -155,21 +155,44 @@ func max(a, b int) int {
### Rust:
```rust
-pub fn max_uncrossed_lines(nums1: Vec
+
diff --git a/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md b/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md
index 50287ce2..1982d449 100644
--- a/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md
+++ b/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md
@@ -200,4 +200,3 @@ class Solution {
-
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 c86376d3..e28bfd04 100644
--- a/problems/动态规划总结篇.md
+++ b/problems/动态规划总结篇.md
@@ -40,7 +40,7 @@
好啦,我们再一起回顾一下,动态规划专题中我们都讲了哪些内容。
-## 动划基础
+## 动态规划基础
* [关于动态规划,你该了解这些!](https://programmercarl.com/动态规划理论基础.html)
* [动态规划:斐波那契数](https://programmercarl.com/0509.斐波那契数.html)
@@ -136,4 +136,3 @@
-
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/周总结/20201107回溯周末总结.md b/problems/周总结/20201107回溯周末总结.md
index 76bd331b..3f1d1012 100644
--- a/problems/周总结/20201107回溯周末总结.md
+++ b/problems/周总结/20201107回溯周末总结.md
@@ -17,7 +17,7 @@
如果是多个集合取组合,各个集合之间相互不影响,那么就不用startIndex,例如:[回溯算法:电话号码的字母组合](https://programmercarl.com/0017.电话号码的字母组合.html)
-**注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路,后面我再讲解排列的时候就重点介绍**。
+**注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路,后面我在讲解排列的时候会重点介绍**。
最后还给出了本题的剪枝优化,如下:
diff --git a/problems/哈希表总结.md b/problems/哈希表总结.md
index 67506363..465ef9d1 100644
--- a/problems/哈希表总结.md
+++ b/problems/哈希表总结.md
@@ -16,7 +16,7 @@
**一般来说哈希表都是用来快速判断一个元素是否出现集合里**。
-对于哈希表,要知道**哈希函数**和**哈希碰撞**在哈希表中的作用.
+对于哈希表,要知道**哈希函数**和**哈希碰撞**在哈希表中的作用。
哈希函数是把传入的key映射到符号表的索引上。
@@ -88,7 +88,7 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底
map是一种`
-
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参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+ # 动态规划:01背包理论基础(滚动数组) + ## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透0-1背包问题!(滚动数组)](https://www.bilibili.com/video/BV1BU4y177kY/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 diff --git a/problems/链表总结篇.md b/problems/链表总结篇.md index dacd4dee..3f2f5c97 100644 --- a/problems/链表总结篇.md +++ b/problems/链表总结篇.md @@ -3,6 +3,7 @@参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+ # 链表总结篇