Leetcode_med 55. 跳跃游戏

描述

给定一个非负整数数组,你最初位于数组的第一个位置。

数组中的每个元素代表你在该位置可以跳跃的最大长度。

判断你是否能够到达最后一个位置。

示例 1:

输入: [2,3,1,1,4]
输出: true
解释: 从位置 0 到 1 跳 1 步, 然后跳 3 步到达最后一个位置。

示例 2:

输入: [3,2,1,0,4]
输出: false
解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。

Python

Greed策略,solution详解:
https://leetcode.com/problems/jump-game/solution/

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        index = lastPos = len(nums)-1
        while index >= 0:
            if nums[index]+index >= lastPos:
                lastPos = index
            index -= 1
        return lastPos == 0
        
全部评论

相关推荐

会员标识
01-20 15:30
牛客运营
emo的打工鸭又被画饼了:关刀划过红玫瑰,爱我尧家英雄会! 都多余了奥!爱谁谁!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务