给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
判断你是否能够到达最后一个下标。
列:
输入:nums = [2,3,1,1,4] 输出:true 解释:可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
start, end = 0, 0 # 判断位置,当前能到达的最远位置
n = len(nums) # 数组长度
while start <= end and end < n-1: # 当前位置不得超过能到达的最远位置,最远位置大于最多需要的步数则跳出判断
end = max(end, nums[start] + start) # 取上一层循环能达到的最远位置和当前能达到的最远位置中的最大值
start += 1 # 判断下一个值
return end >= n-1 # 最远位置超过到达最后一位下标则返回True,反之返回False



