#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
'''
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
target = len(nums) -
max_index =
for index,val in enumerate(nums[:-]):
if max_index <= index and val == :#index之前最遠到index處,val == 0,無法繼續前進了
return False
tmp = index + val
if tmp > max_index: #計算能到達的最遠距離
max_index = tmp
if max_index >= target:
return True
return True
if __name__ == "__main__":
s = Solution()
print s.canJump([,,,,])
print s.canJump([,,,,])
print s.canJump([,,,,])
print s.canJump([])