zoukankan      html  css  js  c++  java
  • [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))
  • 相关阅读:
    【题解】B进制星球
    高斯消元
    7.16
    题解 P1572 【计算分数】
    7.30
    7.31
    项目中使用 MyBatis(一)
    从整体上了解Spring MVC
    Spring注解
    Spring IOC 和 DI
  • 原文地址:https://www.cnblogs.com/loadofleaf/p/5084100.html
Copyright © 2011-2022 走看看