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))
  • 相关阅读:
    【月度盘点】《金秋10月》
    selenium简单使用
    数据解析模块BeautifulSoup简单使用
    爬虫简介
    SQLAlchemy简介
    Flask Blueprint
    Flask基于websocket的简单聊天室
    Flask send_file request
    初识Flask
    Python pip简单使用
  • 原文地址:https://www.cnblogs.com/loadofleaf/p/5084100.html
Copyright © 2011-2022 走看看