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 (跳跃游戏)的更多相关文章

  1. [LeetCode] 55. Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. 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 ...

  3. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  4. [LeetCode] Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. 【LeetCode每天一题】Jump Game(跳跃游戏)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. LeetCode(55): 跳跃游戏

    Medium! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...

  7. Jump Game 的三种思路 - leetcode 55. Jump Game

    Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...

  8. [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 ...

  9. LeetCode: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

随机推荐

  1. (java web后端方向)如何让你的简历为你争取到更多的面试机会,内容来自java web轻量级开发面试教程

    我们在做培训时,会发现一个不合理的情况,一些程序员能力不错,在公司里也是技术牛人,但发出去的简历往往会石沉大海,没有回复.对于刚毕业的大学生或工作年限在2年之内的程序员,这个情况会更严重. 这种情况下 ...

  2. oracle 删除外键约束 禁用约束 启用约束

    oracle 删除外键约束 禁用约束 启用约束 执行以下sql生成的语句即可 删除所有外键约束 Sql代码  select 'alter table '||table_name||' drop con ...

  3. 字符、字符集、编码,以及它们python中会遇到的一些问题(下)

    在看了很多的博客文章之后,总结整理得到了以下文章,非常感谢这些无私奉献的博主! 文章末尾有本文引用的文章的链接,如果有漏掉的文章引用,可以发邮件联系我,随后再次附上链接! 侵删!!! 这一部分是下篇, ...

  4. 再说AutoComplete

    一.简述 昨天support一同事,帮她的客户做类似下面的效果(自动完成): 以前在搜房的时候,弄过这个,调用楼盘字典: 这是一个小功能,也是一个大功能.因为它可以做大,也可以做小. 二.搜房的Aut ...

  5. myeclipse一些快捷键 错了或者没说到补充下

    Ctrl + 1 快速修复Ctrl + D  删除当前行 Ctrl + Alt + ↓ 复制当前行到下一行(复制增加)Ctrl + Alt + ↑ 复制当前行到上一行(复制增加)Alt + ↓ 当前行 ...

  6. [js高手之路] html5 canvas系列教程 - 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)

    接着上文线条样式[js高手之路] html5 canvas系列教程 - 线条样式(lineWidth,lineCap,lineJoin,setLineDash)继续. canvas提供两种输出文本的方 ...

  7. 《MATLAB从入门到放弃》二维曲线和图形绘制基础(二):使用Help文档学习line、plot、plotyy、subplot、hold绘图函数

    目录: »  plot 最常用的二维曲线绘图函数 >  帮助文档 >  基本使用语法 >  线条的样式.符号和颜色调整 >  图形属性调整 >  使用图形句柄进行设置 » ...

  8. const的用法,特别是用在函数前面与后面的区别!

    const的用法,特别是用在函数后面 在普通的非 const成员函数中,this的类型是一个指向类类型的 const指针.可以改变this所指向的值,但不能改变 this所保存的地址. 在 const ...

  9. localStorage和sessionStorage总结以及区别

    (1)兼容的手机和浏览器: (2)使用 .setItem( key, value)存键值数据 sessionStorage.setItem("key","value&qu ...

  10. 前端基础之初识 HTML

    HTML HTML(Hypertext Markup Language)即超文本标记语言,是WWW的描述语言.设计HTML语言的目的是为了能把存放在一台电脑中的文本或图形与另一台电脑中的文本或图形方便 ...