class Solution:
def jump(self, nums: List[int]) -> int:
farthest = 0
# 当前索引能跳到的最远位置,初始为0,跳到了这个位置要更新jump参数以及end参数为当前farthest的地方
end = 0
jumps = 0
for i in range(len(nums)-1):
farthest = max(farthest,nums[i]+i)
if end == i:
jumps += 1
end = farthest
return jumps
每日一道Leetcode - 45. 跳跃游戏 II 【贪心算法】
最新推荐文章于 2024-08-23 16:53:34 发布