Merge branch 'youngyangyang04:master' into master

This commit is contained in:
fwqaaq
2023-07-30 20:00:08 +08:00
committed by GitHub
133 changed files with 1560 additions and 764 deletions

View File

@ -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){
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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) {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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<int> 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>) -> 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>) -> 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>) -> 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>) -> 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
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -310,3 +310,4 @@ class Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -16,11 +16,13 @@
- 输出: 6
- 解释:  连续子数组  [4,-1,2,1] 的和最大,为  6。
# 视频讲解
## 算法公开课
**《代码随想录》算法视频公开课:[贪心算法的巧妙需要慢慢体会LeetCode53. 最大子序和](https://www.bilibili.com/video/BV1aY4y1Z7ya),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[贪心算法的巧妙需要慢慢体会LeetCode53. 最大子序和](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)

View File

@ -17,7 +17,7 @@
## 算法公开课
**《代码随想录》算法视频公开课:[看起来复杂,其实是简单动态规划 | LeetCode53.最大子序和](https://www.bilibili.com/video/BV19V4y1F7b5),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[看起来复杂,其实是简单动态规划 | LeetCode53.最大子序和](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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -6,7 +6,7 @@
## 54.螺旋矩阵
# 54.螺旋矩阵
[力扣题目链接](https://leetcode.cn/problems/spiral-matrix/)

View File

@ -26,9 +26,9 @@
- 输出: false
- 解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 所以你永远不可能到达最后一个位置。
# 视频讲解
## 算法公开课
**《代码随想录》算法视频公开课:[贪心算法,怎么跳跃不重要,关键在覆盖范围 | LeetCode55.跳跃游戏](https://www.bilibili.com/video/BV1VG4y1X7kB),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[贪心算法,怎么跳跃不重要,关键在覆盖范围 | LeetCode55.跳跃游戏](https://www.bilibili.com/video/BV1VG4y1X7kB),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路

View File

@ -22,9 +22,9 @@
* 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
* 注意输入类型已于2019年4月15日更改。 请重置默认代码定义以获取新方法签名。
# 视频讲解
## 算法公开课
**《代码随想录》算法视频公开课:[贪心算法合并区间有细节LeetCode56.合并区间](https://www.bilibili.com/video/BV1wx4y157nD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[贪心算法合并区间有细节LeetCode56.合并区间](https://www.bilibili.com/video/BV1wx4y157nD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@ -341,3 +341,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

View File

@ -50,9 +50,9 @@
* 1 <= m, n <= 100
* 题目数据保证答案小于等于 2 * 10^9
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划中如何初始化很重要!| LeetCode62.不同路径](https://www.bilibili.com/video/BV1ve4y1x7Eu/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划中如何初始化很重要!| LeetCode62.不同路径](https://www.bilibili.com/video/BV1ve4y1x7Eu/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路

View File

@ -46,9 +46,9 @@
* 1 <= m, n <= 100
* obstacleGrid[i][j] 为 0 或 1
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划,这次遇到障碍了| LeetCode63. 不同路径 II](https://www.bilibili.com/video/BV1Ld4y1k7c6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划,这次遇到障碍了| LeetCode63. 不同路径 II](https://www.bilibili.com/video/BV1Ld4y1k7c6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -40,8 +40,8 @@ exection -> execution (插入 'u')
* 0 <= word1.length, word2.length <= 500
* word1 和 word2 由小写英文字母组成
# 算法公开课
**《代码随想录》算法视频公开课:[动态规划终极绝杀! LeetCode72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 算法公开课
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划终极绝杀! LeetCode72.编辑距离](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){
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -343,8 +343,32 @@ public:
## 其他语言版本
### Java
### Java
未剪枝优化
```java
class Solution {
List<List<Integer>> result= new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> 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<List<Integer>> result = new ArrayList<>();

View File

@ -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>) -> 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>) -> 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>) -> 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>) -> 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
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -3,6 +3,7 @@
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
# 90.子集II
[力扣题目链接](https://leetcode.cn/problems/subsets-ii/)

View File

@ -16,9 +16,9 @@
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210113161941835.png)
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划找到子状态之间的关系很重要!| LeetCode96.不同的二叉搜索树](https://www.bilibili.com/video/BV1eK411o7QA/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划找到子状态之间的关系很重要!| LeetCode96.不同的二叉搜索树](https://www.bilibili.com/video/BV1eK411o7QA/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -24,11 +24,9 @@
* 输出0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
# 算法公开课
**《代码随想录》算法视频公开课:[动态规划之 LeetCode121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 算法公开课
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划之 LeetCode121.买卖股票的最佳时机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:
> 贪心

View File

@ -37,9 +37,9 @@
- 1 <= prices.length <= 3 \* 10 ^ 4
- 0 <= prices[i] <= 10 ^ 4
# 视频讲解
## 算法公开课
**《代码随想录》算法视频公开课:[贪心算法也能解决股票问题LeetCode122.买卖股票最佳时机 II](https://www.bilibili.com/video/BV1ev4y1C7na),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[贪心算法也能解决股票问题LeetCode122.买卖股票最佳时机 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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -34,9 +34,9 @@
* 1 <= prices.length <= 3 * 10 ^ 4
* 0 <= prices[i] <= 10 ^ 4
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划,股票问题第二弹 | LeetCode122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划,股票问题第二弹 | LeetCode122.买卖股票的最佳时机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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -39,9 +39,9 @@
* 1 <= prices.length <= 10^5
* 0 <= prices[i] <= 10^5
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划,股票至多买卖两次,怎么求? | LeetCode123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划,股票至多买卖两次,怎么求? | LeetCode123.买卖股票最佳时机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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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<String> 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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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){
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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) {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -28,9 +28,9 @@
* 输出: 4
* 解释: 你可以分别给这三个孩子分发 1、2、1 颗糖果。第三个孩子只得到 1 颗糖果,这已满足上述两个条件。
# 视频讲解
## 算法公开课
**《代码随想录》算法视频公开课:[贪心算法两者兼顾很容易顾此失彼LeetCode135.分发糖果](https://www.bilibili.com/video/BV1ev4y1r7wN),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[贪心算法两者兼顾很容易顾此失彼LeetCode135.分发糖果](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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -33,9 +33,9 @@
* 输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
* 输出: false
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[你的背包如何装满?| LeetCode139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[你的背包如何装满?| LeetCode139.单词拆分](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<String> 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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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){
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -31,9 +31,9 @@
* 0 <= prices.length <= 1000
* 0 <= prices[i] <= 1000
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划来决定最佳时机至多可以买卖K次| LeetCode188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划来决定最佳时机至多可以买卖K次| LeetCode188.买卖股票最佳时机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 {

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -31,9 +31,9 @@
* 0 <= nums.length <= 100
* 0 <= nums[i] <= 400
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划,偷不偷这个房间呢?| LeetCode198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划,偷不偷这个房间呢?| LeetCode198.打家劫舍](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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -197,7 +197,6 @@ class Solution {
}
```
## 其他语言版本
### Python
BFS solution
```python
@ -244,3 +243,4 @@ class Solution:
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
```

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -31,9 +31,9 @@
* 1 <= nums.length <= 100
* 0 <= nums[i] <= 1000
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划,房间连成环了那还偷不偷呢?| LeetCode213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划,房间连成环了那还偷不偷呢?| LeetCode213.打家劫舍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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -432,3 +432,4 @@ function reverseList(head: ListNode | null): ListNode | null {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -28,9 +28,9 @@
提示:
* 1 <= n <= 10^4
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[换汤不换药!| LeetCode279.完全平方数](https://www.bilibili.com/video/BV12P411T7Br/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[换汤不换药!| LeetCode279.完全平方数](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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -4,9 +4,7 @@
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
# 动态规划:一样的套路,再求一次完全平方数
# 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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -33,7 +33,7 @@
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划之子序列问题,元素不连续!| LeetCode300.最长递增子序列](https://www.bilibili.com/video/BV1ng411J7xP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划之子序列问题,元素不连续!| LeetCode300.最长递增子序列](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>) -> i32 {
let mut dp = vec![1; nums.len() + 1];
@ -307,13 +310,8 @@ pub fn length_of_lis(nums: Vec<i32>) -> i32 {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -20,9 +20,9 @@
* 输出: 3
* 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次有冷冻期!| LeetCode309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划来决定最佳时机,这次有冷冻期!| LeetCode309.买卖股票的最佳时机含冷冻期](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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -39,9 +39,9 @@
* 1 <= coins[i] <= 2^31 - 1
* 0 <= amount <= 10^4
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[装满背包最少的物品件数是多少?| LeetCode322.零钱兑换](https://www.bilibili.com/video/BV14K411R7yv/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[装满背包最少的物品件数是多少?| LeetCode322.零钱兑换](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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -16,9 +16,9 @@
![337.打家劫舍III](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223173849619.png)
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划,房间连成树了,偷不偷呢?| LeetCode337.打家劫舍3](https://www.bilibili.com/video/BV1H24y1Q7sY),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划,房间连成树了,偷不偷呢?| LeetCode337.打家劫舍3](https://www.bilibili.com/video/BV1H24y1Q7sY),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@ -523,3 +523,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -22,9 +22,9 @@
* 说明: 你可以假设 n 不小于 2 且不大于 58。
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划,本题关键在于理解递推公式!| LeetCode343. 整数拆分](https://www.bilibili.com/video/BV1Mg411q7YJ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划,本题关键在于理解递推公式!| LeetCode343. 整数拆分](https://www.bilibili.com/video/BV1Mg411q7YJ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@ -473,3 +473,4 @@ object Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -33,11 +33,13 @@
- 输入: [1,2,3,4,5,6,7,8,9]
- 输出: 2
# 视频讲解
## 算法公开课
**《代码随想录》算法视频公开课:[贪心算法,寻找摆动有细节!| LeetCode376.摆动序列](https://www.bilibili.com/video/BV17M411b7NS),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[贪心算法,寻找摆动有细节!| LeetCode376.摆动序列](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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -31,9 +31,9 @@
因此输出为 7。
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[装满背包有几种方法?求排列数?| LeetCode377.组合总和IV](https://www.bilibili.com/video/BV1V14y1n7B6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[装满背包有几种方法?求排列数?| LeetCode377.组合总和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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -28,9 +28,9 @@
两个字符串都只由小写字符组成。
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划,用相似思路解决复杂问题 | LeetCode392.判断子序列](https://www.bilibili.com/video/BV1tv4y1B7ym/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划,用相似思路解决复杂问题 | LeetCode392.判断子序列](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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -37,9 +37,9 @@
题目数据确保队列可以被重建
# 视频讲解
## 算法公开课
**代码随想录算法视频公开课[贪心算法,不要两边一起贪,会顾此失彼 | LeetCode406.根据身高重建队列](https://www.bilibili.com/video/BV1EA411675Y)相信结合视频在看本篇题解更有助于大家对本题的理解**。
**[代码随想录算法视频公开课](https://programmercarl.com/other/gongkaike.html)[贪心算法,不要两边一起贪,会顾此失彼 | LeetCode406.根据身高重建队列](https://www.bilibili.com/video/BV1EA411675Y)相信结合视频在看本篇题解更有助于大家对本题的理解**。
## 思路
@ -402,3 +402,4 @@ object Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -4,7 +4,7 @@
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 416. 分割等和子集
# 416. 分割等和子集
[力扣题目链接](https://leetcode.cn/problems/partition-equal-subset-sum/)
@ -30,9 +30,9 @@
* 1 <= nums.length <= 200
* 1 <= nums[i] <= 100
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划之背包问题,这个包能装满吗?| LeetCode416.分割等和子集](https://www.bilibili.com/video/BV1rt4y1N7jE/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划之背包问题,这个包能装满吗?| LeetCode416.分割等和子集](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

View File

@ -30,9 +30,9 @@
* 输出: 0
* 解释: 你不需要移除任何区间,因为它们已经是无重叠的了。
# 视频讲解
## 算法公开课
**《代码随想录》算法视频公开课:[贪心算法,依然是判断重叠区间 | LeetCode435.无重叠区间](https://www.bilibili.com/video/BV1A14y1c7E1),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[贪心算法,依然是判断重叠区间 | LeetCode435.无重叠区间](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)非常像,弓箭的数量就相当于是非交叉区间的数量,只要把弓箭那道题目代码里射爆气球的判断条件加个等号(认为[01][12]不是相邻区间),然后用总区间数减去弓箭数量 就是要移除的区间数量了。
@ -311,7 +311,7 @@ func min(a, b int) int {
}
```
### Javascript:
### Javascript
- 按右边界排序
```Javascript
var eraseOverlapIntervals = function(intervals) {
@ -447,3 +447,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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:

View File

@ -42,9 +42,9 @@
* points[i].length == 2
* -2^31 <= xstart < xend <= 2^31 - 1
# 视频讲解
## 算法公开课
**代码随想录算法视频公开课[贪心算法,判断重叠区间问题 | LeetCode452.用最少数量的箭引爆气球](https://www.bilibili.com/video/BV1SA41167xe)相信结合视频在看本篇题解更有助于大家对本题的理解**。
**[代码随想录算法视频公开课](https://programmercarl.com/other/gongkaike.html)[贪心算法,判断重叠区间问题 | LeetCode452.用最少数量的箭引爆气球](https://www.bilibili.com/video/BV1SA41167xe)相信结合视频在看本篇题解更有助于大家对本题的理解**。
## 思路

View File

@ -30,9 +30,9 @@
- 0 <= s.length <= 3 \* 10^4
- 1 <= g[i], s[j] <= 2^31 - 1
# 视频讲解
## 算法公开课
**《代码随想录》算法视频公开课:[贪心算法,你想先喂哪个小孩?| LeetCode455.分发饼干](https://www.bilibili.com/video/BV1MM411b7cq),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[贪心算法,你想先喂哪个小孩?| LeetCode455.分发饼干](https://www.bilibili.com/video/BV1MM411b7cq),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路

View File

@ -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) {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -34,9 +34,9 @@
* strs[i] 仅由 '0' 和 '1' 组成
* 1 <= m, n <= 100
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[装满这个背包最多用多少个物品?| LeetCode474.一和零](https://www.bilibili.com/video/BV1rW4y1x7ZQ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[装满这个背包最多用多少个物品?| LeetCode474.一和零](https://www.bilibili.com/video/BV1rW4y1x7ZQ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@ -538,3 +538,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -37,9 +37,9 @@
* 初始的数组的和不会超过 1000 。
* 保证返回的最终结果能被 32 位整数存下。
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[装满背包有多少种方法?| LeetCode494.目标和](https://www.bilibili.com/video/BV1o8411j73x/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[装满背包有多少种方法?| LeetCode494.目标和](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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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<i32>, nums2: Vec<i32>) -> Vec<i32> {
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
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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<i32>) -> Vec<i32> {
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()
}
}
```
<p align="center">

View File

@ -32,9 +32,9 @@ F(n) = F(n - 1) + F(n - 2),其中 n > 1
* 0 <= n <= 30
# 视频讲解
## 算法公开课
**《代码随想录》算法视频公开课:[手把手带你入门动态规划 | leetcode509.斐波那契数](https://www.bilibili.com/video/BV1f5411K7mo),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[手把手带你入门动态规划 | leetcode509.斐波那契数](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
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -41,9 +41,9 @@
* 硬币种类不超过 500 种
* 结果符合 32 位符号整数
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[装满背包有多少种方法?组合与排列有讲究!| LeetCode518.零钱兑换II](https://www.bilibili.com/video/BV1KM411k75j/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[装满背包有多少种方法?组合与排列有讲究!| LeetCode518.零钱兑换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>) -> 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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -17,9 +17,9 @@
* 解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea"
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课[动态规划之子序列,还是为了编辑距离做铺垫 | LeetCode583.两个字符串的删除操(https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[LeetCode583.两个字符串的删除操](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版本一

View File

@ -19,7 +19,7 @@
注意: 合并必须从两个树的根节点开始。
# 算法公开课
## 算法公开课
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[一起操作两个二叉树?有点懵!| LeetCode617.合并二叉树](https://www.bilibili.com/video/BV1m14y1Y7JK),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
@ -793,3 +793,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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) {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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) {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -29,7 +29,7 @@
## 算法公开课
**代码随想录算法视频公开课[动态规划之子序列问题,重点在于连续!| LeetCode674.最长连续递增序列](https://www.bilibili.com/video/BV1bD4y1778v)相信结合视频再看本篇题解更有助于大家对本题的理解**。
**[代码随想录算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划之子序列问题,重点在于连续!| LeetCode674.最长连续递增序列](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>) -> i32 {
if nums.is_empty() {
@ -320,7 +322,28 @@ pub fn find_length_of_lcis(nums: Vec<i32>) -> i32 {
}
```
Javascript
> 贪心
```rust
impl Solution {
pub fn find_length_of_lcis(nums: Vec<i32>) -> 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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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) {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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) {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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:
以上两种写法的区别,我在题解: [DFSBDF 你没注意的细节都给你列出来了LeetCode200. 岛屿数量](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:
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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<int> 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<int> 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;
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -32,9 +32,9 @@
* 0 < prices[i] < 50000.
* 0 <= fee < 50000.
# 算法公开课
## 算法公开课
**代码随想录算法视频公开课[动态规划来决定最佳时机,这次含手续费!| LeetCode714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR)相信结合视频再看本篇题解更有助于大家对本题的理解**。
**[代码随想录算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划来决定最佳时机,这次含手续费!| LeetCode714.买卖股票的最佳时机含手续费](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<i32>, fee: i32) -> i32 {

View File

@ -25,9 +25,7 @@
## 算法公开课
**代码随想录算法视频公开课[动态规划之子序列问题想清楚DP数组的定义 | LeetCode718.最长重复子数组](https://www.bilibili.com/video/BV178411H7hV)相信结合视频再看本篇题解更有助于大家对本题的理解**。
**[代码随想录算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划之子序列问题想清楚DP数组的定义 | LeetCode718.最长重复子数组](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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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<i32>) -> Vec<i32> {
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
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -37,9 +37,9 @@
* cost[i] 将会是一个整型数据,范围为 [0, 999] 。
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划开更了!| LeetCode746. 使用最小花费爬楼梯](https://www.bilibili.com/video/BV16G411c7yZ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划开更了!| LeetCode746. 使用最小花费爬楼梯](https://www.bilibili.com/video/BV16G411c7yZ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-----------
@ -523,3 +523,4 @@ public class Solution
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -24,9 +24,9 @@
* S的长度在[1, 500]之间。
* S只包含小写字母 'a' 到 'z' 。
# 视频讲解
## 算法公开课
**《代码随想录》算法视频公开课:[贪心算法,寻找最远的出现位置! LeetCode763.划分字母区间](https://www.bilibili.com/video/BV18G4y1K7d5),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[贪心算法,寻找最远的出现位置! LeetCode763.划分字母区间](https://www.bilibili.com/video/BV18G4y1K7d5),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@ -409,3 +409,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -50,9 +50,9 @@
* 0 <= bills.length <= 10000
* bills[i] 不是 5 就是 10 或是 20 
# 视频讲解
## 算法公开课
**《代码随想录》算法视频公开课:[贪心算法看上去复杂其实逻辑都是固定的LeetCode860.柠檬水找零](https://www.bilibili.com/video/BV12x4y1j7DD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[贪心算法看上去复杂其实逻辑都是固定的LeetCode860.柠檬水找零](https://www.bilibili.com/video/BV12x4y1j7DD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@ -403,3 +403,4 @@ object Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -147,8 +147,6 @@ class Solution {
}
```
### java
```java
//方法一:采用额外的数组空间
class Solution {
@ -384,3 +382,4 @@ function sortArrayByParityII(nums: number[]): number[] {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -34,9 +34,9 @@
* 1 <= K <= 10000
* -100 <= A[i] <= 100
# 视频讲解
## 算法公开课
**《代码随想录》算法视频公开课:[贪心算法这不就是常识还能叫贪心LeetCode1005.K次取反后最大化的数组和](https://www.bilibili.com/video/BV138411G7LY),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[贪心算法这不就是常识还能叫贪心LeetCode1005.K次取反后最大化的数组和](https://www.bilibili.com/video/BV138411G7LY),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路

View File

@ -19,7 +19,7 @@
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划之子序列问题,换汤不换药 | LeetCode1035.不相交的线](https://www.bilibili.com/video/BV1h84y1x7MP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划之子序列问题,换汤不换药 | LeetCode1035.不相交的线](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<i32>, nums2: Vec<i32>) -> i32 {
@ -173,7 +173,7 @@ pub fn max_uncrossed_lines(nums1: Vec<i32>, nums2: Vec<i32>) -> 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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -35,9 +35,9 @@
* 1 <= stones.length <= 30
* 1 <= stones[i] <= 1000
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[这个背包最多能装多少LeetCode1049.最后一块石头的重量II](https://www.bilibili.com/video/BV14M411C7oV/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[这个背包最多能装多少LeetCode1049.最后一块石头的重量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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -39,7 +39,7 @@
## 算法公开课
**《代码随想录》算法视频公开课:[动态规划子序列问题经典题目 | LeetCode1143.最长公共子序列](https://www.bilibili.com/video/BV1ye4y1L7CQ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[动态规划子序列问题经典题目 | LeetCode1143.最长公共子序列](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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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 {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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) {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

Some files were not shown because too many files have changed in this diff Show More