0
点赞
收藏
分享

微信扫一扫

跳跃游戏--Python解法

给定一个长度为 n 的 0 索引整数数组 nums。初始位置为 nums[0]。 每个元素 nums[i] 表示从索引 i 向前跳转的最大长度。换句话说,如果你在 nums[i] 处,你可以跳转到任意 nums[i + j] 处: 0 <= j <= nums[i]  i + j < n 返回到达 nums[n - 1] 的最小跳跃次数。生成的测试用例可以到达 nums[n - 1]。

def jump(self, nums: List[int]) -> int:
        step,end,max_bound=0,0,0
        for i in range(len(nums)-1):
            max_bound=max(max_bound,nums[i]+i)
            if i==end:
                step+=1
                end=max_bound
        return step
举报

相关推荐

生命游戏Python解法

跳跃游戏

跳跃游戏 II

跳跃游戏IV

跳跃游戏2

跳跃游戏(优化

0 条评论