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.

Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

题意:

思路:

以 [2,3,1,1,4]为例

可以想象一只青蛙,在每个刻度的石头上,弹跳力分别为2,3,1,1,4。青蛙是否能安全过河。

用maxReachIdx记录当前能跳的最远距离

指针i边扫边更新maxReachIdx

若maxReachIdx >= nums.length - 1 则返回true。

例子走一遍,先初始化指针 i= 0 , maxReachIdx = 0

当 i = 0 时,进入for循环,maxReachIdx更新为 3, 可以把红色部分看为青蛙可跳的势力范围

当 i = 4,  maxReachIdx仍然为 3 则maxReachIdx  < nums.length - 1 返回false 。

代码:

   public boolean canJump(int[] nums) {
int maxReachIdx = 0;
for (int i = 0; i < nums.length && i <= maxReachIdx; i++){
maxReachIdx = Math.max(maxReachIdx, i + nums[i]);
}
return maxReachIdx >= nums.length - 1;
}

[leetcode]55. Jump Game青蛙跳(能否跳到终点)的更多相关文章

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

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

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

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

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

  4. Leetcode 55. Jump Game

    我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...

  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 55. Jump Game (跳跃游戏)

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

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

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

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

  9. LeetCode: 55. Jump Game(Medium)

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

随机推荐

  1. jlet

    项目地址 :  https://github.com/kelin-xycs/jlet jlet jlet 一个 javascript 的 小 Lib 本来想写一个 javascript 的 小 Lib ...

  2. PLC 通讯

    几个之前整理的高级语言与PLC通讯的资源下载链接:三菱:http://blog.sina.com.cn/s/blog_16d7d3ecb0102x6wj.html倍福:http://bbs.elecf ...

  3. 数字证书的理解以及自建CA机构颁发证书

    一.理解什么是数字证书   http://www.cnblogs.com/JeffreySun/archive/2010/06/24/1627247.html    理解数字证书等概念,无数次想好好看 ...

  4. Python小练习(一)

    1:有一个列表,其中包括10个元素,例如这个列表是[1,2,3,4,5,6,7,8,9,0],要求将列表中的每个元素一次向前移动一个位置,第一个元素到列表的最后,然后输出这个列表.最终样式是[2,3, ...

  5. SVN简单使用

    如果是window操作系统,默认安装.右键菜单就会有显示SVN 如果已经配置好SVN,直接确定既可以检出. 如果没有配置,那么会显示下面的验证: 输入用户名和密码即可

  6. go-micro介绍 摘自https://www.cnblogs.com/s0-0s/p/6874800.html

    Micro 架构与设计 翻译自 Micro architecture & design patterns for microservices 注: 原文作者即 Micro 框架的开发者. 过去 ...

  7. react 在 componentWillMount() 中调用异步函数时,componentWillMount() finishes after render()

    刚开始使用 react,很多属性.方法不是很熟.在此记录下我所遇到的问题及解决方法. 我在 componentWillMount() 中调用了一个异步函数,在返回结果中调用 this.setState ...

  8. rest api方式实现对文档库的管理

    写在前面 刚入职一家新公司,在对接app的时候需要获取到某公司的sharepoint上面的文档库,获取文档库列表,团队文档库中的文件和文件夹列表,个人文档库中的文件文件夹列表,及在app端进入文件夹的 ...

  9. WPF canvas设置旋转角度和偏移位置

    /// <summary> /// 设置旋转角度和位置 /// </summary> /// <param name="Angle">角度< ...

  10. python中class的序列化和反序列化

    对于类的序列化:将类的成员变量名和数据作为一对键值对存储在物理内存中,例如 class A(object): def __init__(self): self.a = o self.b = 1 sel ...