mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
Update 0045.跳跃游戏II.md
This commit is contained in:
@ -205,66 +205,81 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
贪心(版本一)
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def jump(self, nums: List[int]) -> int:
|
def jump(self, nums):
|
||||||
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:
|
|
||||||
if len(nums) == 1:
|
if len(nums) == 1:
|
||||||
return 0
|
return 0
|
||||||
curDistance, nextDistance = 0, 0
|
|
||||||
step = 0
|
cur_distance = 0 # 当前覆盖最远距离下标
|
||||||
for i in range(len(nums)-1):
|
ans = 0 # 记录走的最大步数
|
||||||
nextDistance = max(nextDistance, nums[i]+i)
|
next_distance = 0 # 下一步覆盖最远距离下标
|
||||||
if i == curDistance:
|
|
||||||
curDistance = nextDistance
|
for i in range(len(nums)):
|
||||||
step += 1
|
next_distance = max(nums[i] + i, next_distance) # 更新下一步覆盖最远距离下标
|
||||||
return step
|
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
|
```python
|
||||||
# 贪心版本三 - 类似‘55-跳跃游戏’写法
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def jump(self, nums) -> int:
|
def jump(self, nums) -> int:
|
||||||
if len(nums)==1: return 0
|
if len(nums)==1: # 如果数组只有一个元素,不需要跳跃,步数为0
|
||||||
i = 0
|
return 0
|
||||||
count = 0
|
|
||||||
cover = 0
|
i = 0 # 当前位置
|
||||||
while i<=cover:
|
count = 0 # 步数计数器
|
||||||
for i in range(i,cover+1):
|
cover = 0 # 当前能够覆盖的最远距离
|
||||||
cover = max(nums[i]+i,cover)
|
|
||||||
if cover>=len(nums)-1: return count+1
|
while i <= cover: # 当前位置小于等于当前能够覆盖的最远距离时循环
|
||||||
count+=1
|
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
|
```python
|
||||||
# 动态规划做法
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def jump(self, nums: List[int]) -> int:
|
def jump(self, nums: List[int]) -> int:
|
||||||
result = [10**4+1]*len(nums)
|
result = [10**4+1] * len(nums) # 初始化结果数组,初始值为一个较大的数
|
||||||
result[0]=0
|
result[0] = 0 # 起始位置的步数为0
|
||||||
for i in range(len(nums)):
|
|
||||||
for j in range(nums[i]+1):
|
for i in range(len(nums)): # 遍历数组
|
||||||
if i+j<len(nums): result[i+j]=min(result[i+j],result[i]+1)
|
for j in range(nums[i] + 1): # 在当前位置能够跳跃的范围内遍历
|
||||||
#print(result) #打印数组
|
if i + j < len(nums): # 确保下一跳的位置不超过数组范围
|
||||||
return result[-1]
|
result[i + j] = min(result[i + j], result[i] + 1) # 更新到达下一跳位置的最小步数
|
||||||
|
|
||||||
|
return result[-1] # 返回到达最后一个位置的最小步数
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user