LeetCode||55. Jump Game

来源:互联网 发布:淘宝开店 保证金 编辑:程序博客网 时间:2024/05/18 17:57

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        """        reach = 0        i = 0        while i<len(nums) and i<=reach:        reach = max(reach, i+nums[i])        i += 1        return reach>=len(nums)-1


原创粉丝点击