【LeetCode每天一题】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.
Example 1:
Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximumjump length is 0, which makes it impossible to reach the last index. 思路
这道题解决的办法有很多种。例如DFS,动态规划等,但是感觉这样有些复杂了。有另外一种办法是我们设置一个可以记录下当前下标的范围内达到的最大位置,当遍历过程中的下标大于最大的下标,说明不能达到尾部。因此直接返回结果。当遍历完毕之后说明可以达到最后的位置,返回结果。时间复杂度为O(n),空间复杂度为O(1)。
图示步骤
解决代码
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
end = 0 # 当前下标所能达到最远的下标
max_end = 0
for i in range(len(nums)):
if end < i: # 说明不能达到尾部
return False
max_end = max(max_end, nums[i]+i) # 记录达到的最远下标 if i == end: # 将最远下标重新复制。
end = max_end return True
【LeetCode每天一题】Jump Game(跳跃游戏)的更多相关文章
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 055 Jump Game 跳跃游戏
给定一个非负整数数组,您最初位于数组的第一个索引处.数组中的每个元素表示您在该位置的最大跳跃长度.确定是否能够到达最后一个索引.示例:A = [2,3,1,1,4],返回 true.A = [3,2, ...
- Leetcode55. Jump Game跳跃游戏
给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 力扣Leetcode 45. 跳跃游戏 II - 贪心思想
这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...
- LeetCode(45): 跳跃游戏 II
Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- 【LeetCode每天一题】Jump Game II(跳跃游戏II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode力扣45题 跳跃游戏 II
原题目: 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: ...
随机推荐
- 【Log】SLF4J简单入门
SLF4J介绍 SLF4J,即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统.按照官方的说法,SLF4J是一个用于日 ...
- 【转】Python3使用Django2.x的settings文件详解
# -*- coding:utf8 -*- import os # 项目路径 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__ ...
- error $GOPATH: no library found in $GOPATH: rdkafka
安装confluent-kafka-go出错时,请使用brew安装pkg-config,手动go get安装的pkg-config还不行... 错误提示: go get -u github.com/c ...
- sqlyog一些快捷键
http://zhidao.baidu.com/link?url=q5GI6myyUENGkDKfGsz-4P01kbdkJBeSgPFvV3HeNjTTvh9QhTXYx5W0xdcS1P7qxmA ...
- mongo连接拒绝10061原因
首先检查Mongo是否启动: 启动 再次检查mongo配置文件是否允许其他人访问,默认路由是否加上 .进入mongodb安装目录的bin目录,新增mongodb.conf文件,输入 bind_ip=0 ...
- Java代码中解压RAR文件
import java.io.File; import java.io.FileOutputStream; import de.innosystec.unrar.Archive; import de. ...
- 最新最全的Java面试题整理(内附答案)
Java基础知识篇 面向对象和面向过程的区别 面向过程: 优点:性能比面向对象高,因为类调用时需要实例化,开销比较大,比较消耗资源;比如单片机.嵌入式开发.Linux/Unix等一般采用面向过程开发, ...
- SQL开头quoted和ansiNULL
“QUOTED_IDENTIFIER” 当 SET QUOTED_IDENTIFIER 为 ON 时,标识符可以由双引号分隔,而文字必须由单引号分隔. 当 SET QUOTED_IDENTIFIER ...
- iOS开发之--属性关键字以及set和get方法
一.属性分为三大类 1.读写性控制 a.readOnly只读,只会生成get方法,不会生成set方法 b.readWrite可读可写,会生成set方法,也会生成get方法(默认设置) 2.setter ...
- 【AI】PaddlePaddle-Docker运行
1.参考官方安装Docker环境,使用一键安装包安装 https://www.jianshu.com/p/b2766173d754 http://www.paddlepaddle.org/docume ...
解决代码