题目描述
You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.
解题思路
请移步 我之前的博客 Jump game II。
代码
class Solution:
def canJump(self, nums: List[int]) -> bool:
cur = 0
if len(nums) <= 1:
return True
n = len(nums)
while True:
if nums[cur] + cur >= n - 1:
return True
if nums[cur] == 0:
return False
temp = 0
choose = cur
for i in range(cur + 1, nums[cur] + cur + 1):
if i + nums[i] > temp:
cur = i
temp = i + nums[i]