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]
        
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容