Jump Game:
在可选的范围内,选择可以到达最远方的点作为新点,并开始新的搜寻,如果遇到0,且已经是最远可达,那么就无法到达终点。
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if len(nums) == 0:
return False
elif len(nums) == 1:
return True
step = 0
while step < len(nums):
if nums[step] == 0:
return False
elif nums[step] + step >= len(nums) - 1:
return True
else :
gmax = 0
for j in range(nums[step]):
if nums[step+j+1]+step+j+1 > gmax:
gmax = nums[step+j+1]+step+j+1
g = step+j+1
step = g