From d65f7a32da277ab0020c3104c6cc3f51b0e51998 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 00:41:29 -0500 Subject: [PATCH] =?UTF-8?q?Update=200045.=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8FII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0045.跳跃游戏II.md | 115 ++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 50 deletions(-) diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index 8a939582..2f0349b2 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -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 - 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 - return ans -``` - -```python -# 贪心版本二 -class Solution: - def jump(self, nums: List[int]) -> int: + def jump(self, nums): 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 -``` -```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 + cur_distance = 0 # 当前覆盖最远距离下标 + ans = 0 # 记录走的最大步数 + next_distance = 0 # 下一步覆盖最远距离下标 + + for i in range(len(nums)): + 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): + 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 +class Solution: + def jump(self, nums) -> int: + 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