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.)

翻譯:給定一個非負(fù)整數(shù)數(shù)組,您首先被定位在數(shù)組的第一個索引處。數(shù)組中的每個元素都代表你在該位置上的最大跳轉(zhuǎn)長度。你的目標(biāo)是達(dá)到最小跳數(shù)的最后一個指數(shù)。例如:給定的數(shù)組a [ 2,3,1,1,4 ]跳到最后一個索引的最小數(shù)目是2。(從指數(shù)0上升到1,然后是最后一個指數(shù)的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]
        
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容