Skip to content

Commit

Permalink
Merge branch 'youngyangyang04:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
615lyw authored Jun 9, 2023
2 parents 5b4249d + f83bbd4 commit 26b5388
Show file tree
Hide file tree
Showing 56 changed files with 2,156 additions and 685 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,26 @@
4. [单调栈:42.接雨水](./problems/0042.接雨水.md)
5. [单调栈:84.柱状图中最大的矩形](./problems/0084.柱状图中最大的矩形.md)

(持续更新中....)

## 图论

通知:开始更新图论内容,图论部分还没有其他语言版本,欢迎录友们提交PR,成为contributor

### 深搜广搜

* [图论:深度优先搜索理论基础](./problems/图论深搜理论基础.md)
* [图论:797.所有可能的路径](./problems/0797.所有可能的路径.md)
* [图论:广度优先搜索理论基础](./problems/图论广索理论基础.md)
* [图论:200.岛屿数量.深搜版](./problems/0200.岛屿数量.深搜版.md)
* [图论:200.岛屿数量.广搜版](./problems/0200.岛屿数量.广搜版.md)
* [图论:695.岛屿的最大面积](./problems/0695.岛屿的最大面积.md)
* [图论:1020.飞地的数量](./problems/1020.飞地的数量.md)
* [图论:130.被围绕的区域](./problems/0130.被围绕的区域.md)
* [图论:417.太平洋大西洋水流问题](./problems/0417.太平洋大西洋水流问题.md)

(持续更新中....)


## 十大排序

## 数论
Expand Down Expand Up @@ -492,7 +508,7 @@

大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发,CSDN博客专家。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。

加入「代码随想录」刷题小分队(微信群),可以扫下方二维码加我微信
加入「代码随想录」刷题小分队(微信群),可以扫下方二维码,加代码随想录客服微信

如果是已工作,备注:姓名-城市-岗位-组队刷题。如果学生,备注:姓名-学校-年级-组队刷题。**备注没有自我介绍不通过哦**

Expand Down
22 changes: 22 additions & 0 deletions problems/0019.删除链表的倒数第N个节点.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,28 @@ struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {

```
C#:
```csharp
public class Solution {
public ListNode RemoveNthFromEnd(ListNode head, int n) {
ListNode dummpHead = new ListNode(0);
dummpHead.next = head;
var fastNode = dummpHead;
var slowNode = dummpHead;
while(n-- != 0 && fastNode != null)
{
fastNode = fastNode.next;
}
while(fastNode.next != null)
{
fastNode = fastNode.next;
slowNode = slowNode.next;
}
slowNode.next = slowNode.next.next;
return dummpHead.next;
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down
101 changes: 58 additions & 43 deletions problems/0045.跳跃游戏II.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,66 +205,81 @@ class Solution {
```

### Python

贪心(版本一)
```python
class Solution:
def jump(self, nums: List[int]) -> int:
if len(nums) == 1: return 0
ans = 0
curDistance = 0
nextDistance = 0
def jump(self, nums):
if len(nums) == 1:
return 0

cur_distance = 0 # 当前覆盖最远距离下标
ans = 0 # 记录走的最大步数
next_distance = 0 # 下一步覆盖最远距离下标

for i in range(len(nums)):
nextDistance = max(i + nums[i], nextDistance)
if i == curDistance:
if curDistance != len(nums) - 1:
ans += 1
curDistance = nextDistance
if nextDistance >= len(nums) - 1: break
next_distance = max(nums[i] + i, next_distance) # 更新下一步覆盖最远距离下标
if i == cur_distance: # 遇到当前覆盖最远距离下标
ans += 1 # 需要走下一步
cur_distance = next_distance # 更新当前覆盖最远距离下标(相当于加油了)
if next_distance >= len(nums) - 1: # 当前覆盖最远距离达到数组末尾,不用再做ans++操作,直接结束
break

return ans

```
贪心(版本二)

```python
# 贪心版本二
class Solution:
def jump(self, nums: List[int]) -> int:
if len(nums) == 1:
return 0
curDistance, nextDistance = 0, 0
step = 0
for i in range(len(nums)-1):
nextDistance = max(nextDistance, nums[i]+i)
if i == curDistance:
curDistance = nextDistance
step += 1
return step
def jump(self, nums):
cur_distance = 0 # 当前覆盖的最远距离下标
ans = 0 # 记录走的最大步数
next_distance = 0 # 下一步覆盖的最远距离下标

for i in range(len(nums) - 1): # 注意这里是小于len(nums) - 1,这是关键所在
next_distance = max(nums[i] + i, next_distance) # 更新下一步覆盖的最远距离下标
if i == cur_distance: # 遇到当前覆盖的最远距离下标
cur_distance = next_distance # 更新当前覆盖的最远距离下标
ans += 1

return ans

```
贪心(版本三) 类似‘55-跳跃游戏’写法

```python
# 贪心版本三 - 类似‘55-跳跃游戏’写法
class Solution:
def jump(self, nums) -> int:
if len(nums)==1: return 0
i = 0
count = 0
cover = 0
while i<=cover:
for i in range(i,cover+1):
cover = max(nums[i]+i,cover)
if cover>=len(nums)-1: return count+1
count+=1
if len(nums)==1: # 如果数组只有一个元素,不需要跳跃,步数为0
return 0

```
i = 0 # 当前位置
count = 0 # 步数计数器
cover = 0 # 当前能够覆盖的最远距离

while i <= cover: # 当前位置小于等于当前能够覆盖的最远距离时循环
for i in range(i, cover+1): # 遍历从当前位置到当前能够覆盖的最远距离之间的所有位置
cover = max(nums[i]+i, cover) # 更新当前能够覆盖的最远距离
if cover >= len(nums)-1: # 如果当前能够覆盖的最远距离达到或超过数组的最后一个位置,直接返回步数+1
return count+1
count += 1 # 每一轮遍历结束后,步数+1


```
动态规划
```python
# 动态规划做法
class Solution:
def jump(self, nums: List[int]) -> int:
result = [10**4+1]*len(nums)
result[0]=0
for i in range(len(nums)):
for j in range(nums[i]+1):
if i+j<len(nums): result[i+j]=min(result[i+j],result[i]+1)
#print(result) #打印数组
return result[-1]
result = [10**4+1] * len(nums) # 初始化结果数组,初始值为一个较大的数
result[0] = 0 # 起始位置的步数为0

for i in range(len(nums)): # 遍历数组
for j in range(nums[i] + 1): # 在当前位置能够跳跃的范围内遍历
if i + j < len(nums): # 确保下一跳的位置不超过数组范围
result[i + j] = min(result[i + j], result[i] + 1) # 更新到达下一跳位置的最小步数

return result[-1] # 返回到达最后一个位置的最小步数


```

Expand Down
24 changes: 19 additions & 5 deletions problems/0053.最大子序和.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,35 @@ class Solution {
```

### Python
暴力法
```python
class Solution:
def maxSubArray(self, nums):
result = float('-inf') # 初始化结果为负无穷大
count = 0
for i in range(len(nums)): # 设置起始位置
count = 0
for j in range(i, len(nums)): # 从起始位置i开始遍历寻找最大值
count += nums[j]
result = max(count, result) # 更新最大值
return result

```
```python
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
result = -float('inf')
def maxSubArray(self, nums):
result = float('-inf') # 初始化结果为负无穷大
count = 0
for i in range(len(nums)):
count += nums[i]
if count > result:
if count > result: # 取区间累计的最大值(相当于不断确定最大子序终止位置)
result = count
if count <= 0:
if count <= 0: # 相当于重置最大子序起始位置,因为遇到负数一定是拉低总和
count = 0
return result
```


```
### Go

```go
Expand Down
11 changes: 8 additions & 3 deletions problems/0053.最大子序和(动态规划).md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

示例:
输入: [-2,1,-3,4,-1,2,1,-5,4]
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
* 输入: [-2,1,-3,4,-1,2,1,-5,4]
* 输出: 6
* 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。

## 算法公开课

**《代码随想录》算法视频公开课:[看起来复杂,其实是简单动态规划 | LeetCode:53.最大子序和](https://www.bilibili.com/video/BV19V4y1F7b5),相信结合视频再看本篇题解,更有助于大家对本题的理解**


## 思路

Expand Down
22 changes: 14 additions & 8 deletions problems/0056.合并区间.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,24 @@ class Solution {
### Python
```python
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
if len(intervals) == 0: return intervals
intervals.sort(key=lambda x: x[0])
def merge(self, intervals):
result = []
result.append(intervals[0])
if len(intervals) == 0:
return result # 区间集合为空直接返回

intervals.sort(key=lambda x: x[0]) # 按照区间的左边界进行排序

result.append(intervals[0]) # 第一个区间可以直接放入结果集中

for i in range(1, len(intervals)):
last = result[-1]
if last[1] >= intervals[i][0]:
result[-1] = [last[0], max(last[1], intervals[i][1])]
if result[-1][1] >= intervals[i][0]: # 发现重叠区间
# 合并区间,只需要更新结果集最后一个区间的右边界,因为根据排序,左边界已经是最小的
result[-1][1] = max(result[-1][1], intervals[i][1])
else:
result.append(intervals[i])
result.append(intervals[i]) # 区间不重叠

return result

```

### Go
Expand Down
59 changes: 56 additions & 3 deletions problems/0062.不同路径.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,70 @@ public:
```

### Python
递归
```python
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
if m == 1 or n == 1:
return 1
return self.uniquePaths(m - 1, n) + self.uniquePaths(m, n - 1)

```
动态规划(版本一)
```python
class Solution: # 动态规划
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
dp = [[1 for i in range(n)] for j in range(m)]
# 创建一个二维列表用于存储唯一路径数
dp = [[0] * n for _ in range(m)]

# 设置第一行和第一列的基本情况
for i in range(m):
dp[i][0] = 1
for j in range(n):
dp[0][j] = 1

# 计算每个单元格的唯一路径数
for i in range(1, m):
for j in range(1, n):
dp[i][j] = dp[i][j - 1] + dp[i - 1][j]
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]

# 返回右下角单元格的唯一路径数
return dp[m - 1][n - 1]

```
动态规划(版本二)
```python
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
# 创建一个一维列表用于存储每列的唯一路径数
dp = [1] * n

# 计算每个单元格的唯一路径数
for j in range(1, m):
for i in range(1, n):
dp[i] += dp[i - 1]

# 返回右下角单元格的唯一路径数
return dp[n - 1]
```
数论
```python
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
numerator = 1 # 分子
denominator = m - 1 # 分母
count = m - 1 # 计数器,表示剩余需要计算的乘积项个数
t = m + n - 2 # 初始乘积项
while count > 0:
numerator *= t # 计算乘积项的分子部分
t -= 1 # 递减乘积项
while denominator != 0 and numerator % denominator == 0:
numerator //= denominator # 约简分子
denominator -= 1 # 递减分母
count -= 1 # 计数器减1,继续下一项的计算
return numerator # 返回最终的唯一路径数

```
### Go

```Go
Expand Down
Loading

0 comments on commit 26b5388

Please sign in to comment.