Leetcode45-跳跃游戏II
思路
代码:
class Solution:
def jump(self, nums: List[int]) -> int:
if len(nums) == 1:
return 0
step = 0 # 记录总步数
left = 0
right = 0
cur = 0
# nums=[2,3,1,1,4]
while left < len(nums):
left = cur + 1
right = cur + nums[cur]
if right >= len(nums) - 1: #注意>=
return step + 1
tmp = 0 #注意tmp初始化的位置
for i in range(left, right+1):
if i + nums[i] > tmp:
tmp = i + nums[i]
cur = i
step += 1