python leetcode 55. Jump Game

看能否达到最后的条件是

  1. 能达到当前位置i
  2. nums[i]+i能跳到最后

所以用step记录当前还能跳几步,如果当前的步数nums[i]比step大,则更新当前的step

class Solution(object):
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        l1=len(nums)
        if l1 <= 1:
            return True
        step = nums[0]
        for i in range(1,l1):
            if step + i>=l1:
                return True
            if step > 0:
                step -= 1
                step = max(step, nums[i])
            else:
                return False
        return True

猜你喜欢

转载自blog.csdn.net/Neekity/article/details/84874099