45. Jump Game II

原文地址http://www.lintcode.com/problem/jump-game-ii/
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. For example: Given array A = [2,3,1,1,4] The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

翻译:给定一个非负整数数组,您首先被定位在数组的第一个索引处。数组中的每个元素都代表你在该位置上的最大跳转长度。你的目标是达到最小跳数的最后一个指数。例如:给定的数组a [ 2,3,1,1,4 ]跳到最后一个索引的最小数目是2。(从指数0上升到1,然后是最后一个指数的3个步骤。)

class Solution(object):
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        p = [0]
        for i in range(len(nums) - 1):
            while(i + nums[i] >= len(p) and len(p) < len(nums)):
                p.append(p[i] + 1)
        return p[-1]
        

你可能感兴趣的:(45. Jump Game II)