• [LeetCode]题解(python):055-Jump Game



    题目来源


    https://leetcode.com/problems/jump-game/

    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.

    Determine if you are able to reach the last index.

    For example:
    A = [2,3,1,1,4], return true.

    A = [3,2,1,0,4], return false.


    题意分析


    Input: a list

    Output: True or False

    Conditions:每一个位表示能向前几位,看能不能到达终点


    题目思路


    记录每一个位置能最多向前多少,然后不断更新这个最大值,如果能一直循环到最后则说明可以到达


    AC代码(Python)


     1 __author__ = 'YE'
     2 
     3 class Solution(object):
     4     def canJump(self, nums):
     5         """
     6         :type nums: List[int]
     7         :rtype: bool
     8         """
     9         step = nums[0]
    10         for i in range(1, len(nums)):
    11             if step > 0:
    12                 step -= 1
    13                 step = max(step, nums[i])
    14             else:
    15                 return False
    16         return True
    17 
    18 nums = [3,2,1,0,4]
    19 nums1 = [2,3,1,1,4]
    20 print(Solution().canJump(nums1))
  • 相关阅读:
    pip 安装
    「csp模拟」模拟测试15
    某些博客的优化
    晚间测试6
    「csp模拟」模拟测试15
    「csp模拟」模拟测试14
    线段树维护单调栈
    晚间测试 2
    晚间测试 1
    晚间测试4
  • 原文地址:https://www.cnblogs.com/loadofleaf/p/5084100.html
Copyright © 2020-2023  润新知