LeetCode45. 跳跃游戏 II
题目链接
https://leetcode.cn/problems/jump-game-ii/description/
代码
python
class Solution:
def jump(self, nums: List[int]) -> int:
if len(nums) == 1:
return 0
curdis = 0
nextdis = 0
step = 0
for i in range(len(nums)):
nextdis = max(nums[i] + i, nextdis)
if i == curdis:
step += 1
curdis = nextdis
if nextdis >= len(nums) - 1:
break
return step