Leetcode练习(Python):数组类:第55题:给定一个非负整数数组,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断你是否能够到达最后一个位置。

题目:
给定一个非负整数数组,你最初位于数组的第一个位置。  数组中的每个元素代表你在该位置可以跳跃的最大长度。  判断你是否能够到达最后一个位置。
思路:
较简单,与第45题思路类似
程序:
class Solution:
    def canJump(self, nums: List[int]) -> bool:
        length = len(nums)
        if length <= 0:
            return False
        if length == 1:
            return True
        boundary = 0
        auxiliary_length = 0
        for index in range(length):
            auxiliary_length = max(auxiliary_length, nums[index] + index)
            if index == boundary:
                boundary = auxiliary_length
            if boundary >= length - 1:
                return True
                break
        return False
posted on 2020-04-20 13:19  桌子哥  阅读(1091)  评论(0编辑  收藏  举报