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. 解决WPF中异常导致的程序Crash

    通常在WPF中出现异常,会导致程序Crash,即使把异常Throw出来,依旧会报错,解决方法只需在App.xaml.cs中进行处理即可,废话不说,代码如下: private int exception ...

  2. 给大厨写的R数据分析代码

    ###************************************** 新老客户统计 ***************************************### dachu &l ...

  3. 谈谈 ServerFul 架构

    我写了一篇文章 <自己实现一个线程池>  https://www.cnblogs.com/KSongKing/p/9803935.html , 其实 不仅仅 是 线程池, 中间件 层 的 ...

  4. C语言strcmp()实现

    函数原型:    extern int strcmp(const char *s1,const char *s2); 比较两个字符串 设这两个字符串为str1,str2, 若str1=str2,则返回 ...

  5. Day 09 函数基础

    函数初级 简介 # 函数是一系列代码的集合,用来完成某项特定的功能 优点 '''1. 避免代码的冗余2. 让程序代码结构更加清晰3. 让代码具有复用性,便于维护''' 函数四部分 '''1. 函数名: ...

  6. DotNetBar中Supergrid显示树形数据

    1.向窗体中拖一个Supergrid控件 2.添加列ID,NAME,MATH,CN,SEX 3.在任务窗格中勾选“Show Tree Lines”和“Show Tree Buttons” 4.添加数据 ...

  7. npm i 出错

    npm i npm ERR! code ECONNRESET npm ERR! errno ECONNRESET npm ERR! network request to https://registr ...

  8. logback不输出日志消息,且SLF4J绑定源错误

    我之前的项目已经成功使用过logback作为日志输出,但是今天新项目在使用的时候,不输出日志信息. 最后终于找到问题所在,并成功解决.解决步骤如下: 第一步:检查pom.xml 按照以往惯例,我先检查 ...

  9. processjs Documentation

    Documentation   Paul Nieuwelaar edited this page on 20 Sep 2017 · 4 revisions Installation & Usa ...

  10. 刘志梅201771010115.《面向对象程序设计(java)》第三周学习总结

    实验三 Java基本程序设计(2) 实验时间 2018-9-13 1.实验目的与要求 (1)进一步掌握Eclipse集成开发环境下java程序开发基本步骤: (2)熟悉PTA平台线上测试环境: (3) ...