【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 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: ...
随机推荐
- .net Core Abp See config settings - "CustomSchemaIds" for a workaround
Swagger See config settings - "CustomSchemaIds" for a workaround System.InvalidOperationE ...
- JVM核心知识体系(转http://www.cnblogs.com/wxdlut/p/10670871.html)
1.问题 1.如何理解类文件结构布局? 2.如何应用类加载器的工作原理进行将应用辗转腾挪? 3.热部署与热替换有何区别,如何隔离类冲突? 4.JVM如何管理内存,有何内存淘汰机制? 5.JVM执行引擎 ...
- 解决SVN 每次操作都需要重输入用户名密码问题
把目录C:\Users\当前账号\AppData\Roaming\Subversion\auth下的文件删除,然后重启hbuilder或eclipse工具,重新输入账号密码之后,保存即可解决该问题.
- PHP下载远程图片的3个方法
From: http://blog.csdn.net/iefreer/article/details/46930239 直接上代码 <?php function dlfile1($file_ur ...
- MTK 关闭耳机调至最大音量时,提示损伤听力
android开发之耳机调至最大音量时,提示损伤听力 android开发之耳机调至最大音量时,提示损伤听力 通过提示语,我们可以查出,只要的逻辑代码是在framework/base/packages/ ...
- int和Integer有什么区别
Java是一个近乎纯洁的面向对象编程语言,但是为了编程的方便还是引入了基本数据类型,但是为了能够将这些基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型(wrapper cl ...
- python从XML里取数,遍历等
#coding=utf-8 #通过minidom解析xml文件 import xml.dom.minidom as xmldom import os ''' XML文件读取 <?xml vers ...
- php.ini 开发和线上配置的差异
比对了一下php自带的php.ini-development和php.ini-production,备忘. display_errors = Ondisplay_startup_errors = On ...
- 字符串匹配的 KMP算法
一般字符串匹配过程 KMP算法是字符串匹配算法的一种改进版,一般的字符串匹配算法是:从主串(目标字符串)和模式串(待匹配字符串)的第一个字符开始比较,如果相等则继续匹配下一个字符, 如果不相等则从主串 ...
- 解决 nginx 出现 413 Request Entity Too Large 的问题
1.若nginx用所用的 php 请求解析服务是 fpm, 则检查 /etc/php5/fpm/php.ini 文件中的参数 upload_max_filesize = 20M post_max_si ...