https://leetcode-cn.com/problems/jump-game/

# -*- coding:utf-8 -*-

class Solution(object):
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        start = 0
        end = 0
        n = len(nums)
        while start <= end and end < len(nums) - 1:
            end = max(end, nums[start] + start)
            start += 1
        return end >= n - 1
if __name__ == '__main__':
    nums = [3, 2, 1, 0, 4]
    print(Solution().canJump(nums))

思路:标记一个最远跳跃距离,依次判断当前位置加上当前位置最远跳跃距离有没有超过最远距离,在遍历完所有点之后判断最远距离大不大于数组长度

分类: 算法

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用 * 标注