LeetCode #55 Jump Game

题目

Jump Game


解题方法

这道题可以用贪心算法的思想解决,类似于Jump Game II


代码

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        curEnd = 0
        curFarthest = 0
        
        for i in range(len(nums)-1):
            curFarthest = max(curFarthest, i + nums[i])
            if i == curEnd:
                curEnd = curFarthest
                if curEnd >= len(nums) - 1:
                    break
        
        if curEnd >= len(nums) - 1:
            return True
        else:
            return False
posted @ 2020-10-13 16:39  老鼠司令  阅读(94)  评论(0)    收藏  举报