1. 程式人生 > >python leetcode 45. Jump Game II

python leetcode 45. Jump Game II

cur,last分別表示當前這一步,上一步能到達的最大下標位置

  1. 如果當前下標i>last: 步數加1 更新last=cur
  2. 否則:更新cur

由於假設能到最後位置,所以省去了判斷是否能到。

class Solution:
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n=len(nums)
        cur=last=step=0
        for i in range
(n): if i>last: step+=1 last=cur cur=max(cur,i+nums[i]) return step