1. 原题链接

https://leetcode.com/problems/jump-game/description/

2. 题目要求

给定一个整型数组,数组中没有负数。从第一个元素开始,每个元素的值代表每一次你能从当前位置跳跃的步数。问能否跳到该数组的最后一个元素位置

注意:可以跳的步数超出数组长度依旧视为可以达到最后位置

3. 解题思路

从第一个元素开始遍历,记录下你所能到达的最远位置,例如{2, 2, 0, 1, 2},遍历第一个元素时,你所能到达的最远位置是“i+nums[i]”=2,也就是nums[2]。

然后让 “i<2;i++”继续遍历,nums[1]+1=3,可以到达nums[3] =1,nums[3] +1>nums.length-1,返回true

4. 代码实现

public class JumpGame55 {
public static void main(String[] args) {
int[]nums ={2,2,0,2,4};
System.out.println(canJump(nums));
}
public static boolean canJump(int[] nums) {
int dis = 0;
for (int i = 0; i <= dis; i++) {
dis = Math.max(dis, i + nums[i]);
System.out.println(dis);
if (dis >= nums.length-1) {
return true;
}
}
return false;
}
}

  

LeetCode: 55. Jump Game(Medium)的更多相关文章

  1. [leetcode] 55. Jump Game (Medium)

    原题 题目意思即 每一格代表你当前最多能再往后跳几次,从第一格开始,如果能跳到最后一格返回true,反之为false. 思路:用一个下标记录当前最多能跳到哪一格,遍历一遍 --> 如果当前格子不 ...

  2. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  3. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  4. LeetCode:11. ContainerWithWater(Medium)

    原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an  ...

  5. LeetCode 55. Jump Game (跳跃游戏)

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

  6. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...

  7. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

  8. LeetCode: 54. Spiral Matrix(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...

  9. LeetCode:46. Permutations(Medium)

    1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...

随机推荐

  1. JavaScript的DOM_处理空白节点

    一.空白节点产生的原因 在非 IE6,7,8 中,标准的 DOM 具有识别空白文本节点的功能.  在火狐浏览器等其他浏览器中是 7个,而 IE6,7,8 自动忽略了,如果要保持一致的子元素节点,需要手 ...

  2. springMVC框架下返回json格式的对象,list,map

    原文地址:http://liuzidong.iteye.com/blog/1069343 注意这个例子要使用jQuery,但是jquery文件属于静态的资源文件,所以要在springMVC中设置静态资 ...

  3. C# .NET开发图形图像程序时提示"GDI+ 中发生一般性错误"

    今天突然收到一封信,说我那个极度复杂的Marshal的问题被解决了(http://www.cnblogs.com/hotcan/archive/2005/01/12/91007.html).顿时感觉好 ...

  4. CSU-ACM2018暑期训练7-贪心

    A:合并果子(贪心+优先队列) B:HDU 1789 Doing Homework again(非常经典的贪心) C:11572 - Unique Snowflakes(贪心,两指针滑动保存子段最大长 ...

  5. iOS8以后UIAlertView和UIActionSheet两种alert页面都将通过UIAlertController来创建

    1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...

  6. OC - KVO实现原理

    1.KVO简介 KVO是Objective-C对观察者设计模式的一种实现,它提供一种机制,指定一个被观察对象(如A类),当对象中的某个属性发生变化的时候,对象就会接收到通知,并作出相应的处理.在MVC ...

  7. 『ACM C++』 PTA 天梯赛练习集L1 | 048-49

    今日刷题048-049 ------------------------------------------------L1-048---------------------------------- ...

  8. CF1066A Vova and Train(模拟)

    大水题... 题目要求你求能看到的灯笼数,我们可以分为3部分 总共的灯笼数——————1 在 l 左面的灯笼数(不包括lll)——————2 在 r 左面的灯笼数(包括rrr)——————3 我们知道 ...

  9. MySQL---存储过程 及 条件语句、循环语句

    存储过程 存储过程是一个SQL语句集合,当主动去调用存储过程时,其中内部的SQL语句会按照逻辑执行. 1.创建存储过程 -- 创建存储过程 delimiter // create procedure ...

  10. 我的Tmux学习笔记

    0. 修改指令前缀 // ~/.tmux.conf ubind C-b set -g prefix C-a 1. 新建会话 tmux tmux new -s session-name // 可以设置会 ...