博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode55. Jump Game(思路及python解法)
阅读量:2241 次
发布时间:2019-05-09

本文共 872 字,大约阅读时间需要 2 分钟。

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.

Example 1:

Input: [2,3,1,1,4]Output: trueExplanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: [3,2,1,0,4]Output: falseExplanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.

看是否能达到最后一个点。

记录好每次可以到达的最远距离,如果最远距离>=len(nums)直接返回True。

while的条件可以改变。(for循环的时候i不能改)

class Solution:    def canJump(self, nums: List[int]) -> bool:        reach=nums[0]        i=0        while i<=reach:            reach=max(i+nums[i],reach)            if reach>=len(nums)-1:                return True            i+=1        return False

 

转载地址:http://tcrbb.baihongyu.com/

你可能感兴趣的文章
SDO_GEOMETRY结构说明
查看>>
oracle 的 SDO_GEOMETRY
查看>>
往oracle中插入geometry的两种方式
查看>>
Oracle Spatial中的Operator操作子 详细说明
查看>>
Oracle Spatial中SDO_Geometry详细说明
查看>>
oracle 聚合函数 LISTAGG ,将多行结果合并成一行
查看>>
Oracle列转行函数 Listagg() 语法详解及应用实例
查看>>
LISTAGG函数的用法
查看>>
Oracle Spatial操作geometry方法
查看>>
IDEA类和方法注释模板设置(非常详细)
查看>>
Java程序初始化的顺序
查看>>
Dubbo和Spring结合配置文件内容解析为bean的过程
查看>>
fastJson注解@JSONField使用的一个实例
查看>>
fastjson的@JSONField注解的一点问题
查看>>
fastjson使用(三) -- 序列化
查看>>
浅谈使用单元素的枚举类型实现单例模式
查看>>
Java 利用枚举实现单例模式
查看>>
Java 动态代理作用是什么?
查看>>
Java动态代理机制详解(JDK 和CGLIB,Javassist,ASM) (清晰,浅显)
查看>>
三种线程安全的单例模式
查看>>