天天看点

56 leetcode - Jump Game

#!/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([])