zoukankan      html  css  js  c++  java
  • leetcode-mid-dynamic programming-55. Jump Game

    mycode  71.47%

    思路:

    既然要到达终点,那么俺就可以倒推,要想到达n,可以有以下情况

    1)到达n-1,然后该位置最少可以走一步

    2)到达n-2,然后该位置最少可以走两步

    3)到达n-3,然后该位置最少可以走三步

    。。。。

     emmmm...本来想按照这个方法写个函数,发现跑不通。。。。我就干脆先把list倒过来,当下个数大于等于,他能到达上一个数,但是当这个数暂时不能到达时,我就需要给下一个数加一个step的要求啦

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

    参考:

    跟我的差不多,区别:我的目标--不断衡量距离,他的目标--不断改变目的地

    class Solution(object):
        def canJump(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            if not nums or len(nums)==1 : return True
            des = len(nums) - 1
            for i in range(len(nums)-2,-1,-1):
                if nums[i] >= des - i:
                    des = i
            return des == 0 
  • 相关阅读:
    poj2752Seek the Name, Seek the Fame【kmp next数组应用】
    poj1961Period【kmp next数组】
    poj2406(kmp next数组)
    KMP原理
    0529
    0428
    2045年4月25日
    0421
    黄金连分数【大数】
    学习linux内核时常碰到的汇编指令(1)
  • 原文地址:https://www.cnblogs.com/rosyYY/p/10978792.html
Copyright © 2011-2022 走看看