55. Jump Game
网址:https://leetcode.com/problems/jump-game/description/
代码:
class Solution:
def canJump(self, nums):
length = 0
for i in range(len(nums)):
if length < i:
return False
if i + nums[i] > length:
length = i + nums[i]
if length >= len(nums)-1:
return True
else:
return False
思路:用length表示可以到达的最长的点,遍历数组中的值,如果length小于下标i表示无法到达i,返回false,如果i加nums[i]大于length,更新length的值,循环结束后判断能否到达数组结尾
提交情况: