LeetCode(力扣)45. 跳跃游戏 IIPython

LeetCode45. 跳跃游戏 II

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/jump-game-ii/description/
LeetCode(力扣)45. 跳跃游戏 IIPython_第1张图片

代码

class Solution:
    def jump(self, nums: List[int]) -> int:
        if len(nums) == 1:
            return 0
        curdis = 0
        nextdis = 0
        step = 0
        for i in range(len(nums)):
            nextdis = max(nums[i] + i, nextdis)
            if i == curdis:
                step += 1
                curdis = nextdis
                if nextdis >= len(nums) - 1:
                    break
        return step

你可能感兴趣的:(leetcode,游戏,算法)