题目链接:https://leetcode-cn.com/problems/jump-game/
难度:中等

描述:
给定一个非负整数数组 nums ,你最初位于数组的 第一个下标
数组中的每个元素代表你在该位置可以跳跃的最大长度。
判断你是否能够到达最后一个下标。

题解

思路:
假设在坐标i,可以跳跃3个下标,那么意味着i + 1i + 2i + 3都是可以达到的,由此来更新能够达到的最大下标。(i + k + nums[k])。

  1. class Solution:
  2. def canJump(self, nums: List[int]) -> bool:
  3. step = 0
  4. for i in range(len(nums)):
  5. if step < i:
  6. return False
  7. step = max(step, i + nums[i])
  8. return True