LeetCode 55. 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.
题目标签:Array
这道题目给了我们一个array,其中每一个数字代表着可以跳跃的最大步数,让我们判断,能不能跳跃到最后一个数字,这里没有要求要刚刚好跳到最后,超过也可以。一开始自己写了一个递归方法,但是因为速度慢,无法通过全是1的array。所以这道题目用Dynamic Programming方法,遍历array,过程中,我们只需要维护一个值 - 最远能跳跃到的地方 farthestNumIndex。对于每一个数字,我们都算一下它能最远跳跃到哪里,然后和 我们维护的 farthestNumIndex 比较,取大的。所以当我们知道了我们最远能够跳跃到哪里的话,如果遇到了原题中的第二种可能,怎么也跳跃不过0的这种情况,我们只需要判断目前的数字的index i 是不是超过了 farthestNumIndex,超过了,就直接判断为false了。 当farthestNumIndex 达到了,或者超越了最后一位数字的 index,就判断true。
Java Solution:
Runtime beats 39.82%
完成日期:07/19/2017
关键词:Array
关键点:Dynamic Programming - 维护一个跳跃到最远的值
public class Solution
{
public boolean canJump(int[] nums)
{
int farthestNumIndex = 0; // the farthest place we can jump to
boolean res = false; for(int i=0; i<nums.length; i++)
{
if(i > farthestNumIndex) // meaning there is no way to jump further
break; if(farthestNumIndex >= nums.length - 1) // meaning we can jump to or over the last number
{
res = true;
break;
}
// keep tracking the farthest spot we can jump
farthestNumIndex = Math.max(farthestNumIndex, i + nums[i]);
} return res;
} }
参考资料:
http://www.cnblogs.com/grandyang/p/4371526.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 55. Jump Game (跳跃游戏)的更多相关文章
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 55. Jump Game & 45. Jump Game II
55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode每天一题】Jump Game(跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode(55): 跳跃游戏
Medium! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...
- Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
- [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode: 55. Jump Game(Medium)
1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...
随机推荐
- 在linux下通过hexdump生成一个十六进制的文本保存文件,解析此文件转变成正常源代码文件。
举例说明: 此十六进制保存的文件为此源代码hexdump生成的: #include<stdio.h> #include<string.h> #include<stdlib ...
- 浅谈SQL优化入门:1、SQL查询语句的执行顺序
1.SQL查询语句的执行顺序 (7) SELECT (8) DISTINCT <select_list> (1) FROM <left_table> (3) <join_ ...
- 14.LINUX-platform机制实现驱动层分离(详解)
版权声明:本文为博主原创文章,未经博主允许不得转载. 本节目标: 学习platform机制,如何实现驱动层分离 1.先来看看我们之前分析输入子系统的分层概念,如下图所示: 如上图所示,分 ...
- OC——关于KVO
我们知道在WPF.Silverlight中都有一种双向绑定机制,如果数据模型修改了之后会立即反映到UI视图上,类似的还有如今比较流行的基于MVVM设计模式的前端框架,例如Knockout.js.其实在 ...
- BZOJ1854_游戏_KEY
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MB Description lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备, ...
- Opengl4.5 中文手册—E
索引 A B C D E F G H I J K L M N O P Q ...
- 在CentOS6上编译安装实现LAMP(php-modules)+phpMyAdmin安装过程全记录
php与apache协作有三种模式:CGI.modules.FastCGI. 在CGI模式下,用户请求php文件时,apache会启动响应进程,调用php处理器处理请求,然后将结果返回给客户端.用户响 ...
- 一个强迫症的Git 选择
选择 1,经常性的commit or 干净的历史 在本地(私有)的开发分支中,选择经常性的commit,以便于实时记录修改,回退等操作.eg.develop,feature... 实现方式: comm ...
- selenium 调用键盘按键
1.想要调用键盘按键操作需要引入keys包: from selenium.webdriver.common.keys import keys 通过send_keys() 调用按键 send_keys( ...
- 即时通信系统Openfire分析之六:路由表 RoutingTable
还是从会话管理说起 上一章,Session经过预创建.认证之后,才正常可用.认证时,最重要的操作,就是将Session加入到路由表,使之拥用了通信功能. 添加到至路由表的操作,是在SessionMan ...