mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Update 0045.跳跃游戏II.md
This commit is contained in:
@ -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<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] # 返回到达最后一个位置的最小步数
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user