题目:

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.

Your goal is to reach the last index in the minimum number of jumps.

For example:

Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step
from index 0 to 1, then 3 steps to the last index.)

题意:

给定一个非负整数数组。给定的初始化位置在数组的起始位置。

数组中的每一个元素代表着你能都在此位置跳跃的最大的距离。

你的目标是用最少的跳跃数达到数组的末尾。

比方:给定A = [2,3,1,1,4]

达到数组尾部的最小的跳跃步数为2。(用1步从索引 0 到 1, 接着用3步到达结尾索引。)

算法分析:

该题思想主要是。扫描数组,以确定当前最远能覆盖的节点。放入maxreach。

然后继续扫描,直到当前的路程超过了上一次算出的覆盖范围reach,那么更新覆盖范围,同一时候更新条数,由于我们是经过了多一跳才干继续前进的。

形象地说,这个是在争取每跳最远的greedy.

* ret:眼下为止的jump数





    * curRch:从A[0]进行ret次jump之后达到的最大范围





    * curMax:从0~i这i+1个A元素中能达到的最大范围

    

    * 当curRch < i,说明ret次jump已经不足以覆盖当前第i个元素。因此须要添加一次jump,使之达到





    * 记录的curMax。

AC代码:

/**
* ret:眼下为止的jump数 * curRch:从A[0]进行ret次jump之后达到的最大范围 * curMax:从0~i这i+1个A元素中能达到的最大范围 * 当curRch < i,说明ret次jump已经不足以覆盖当前第i个元素,因此须要添加一次jump。使之达到 * 记录的curMax。 */
public class Solution
{
public int jump(int[] nums)
{
int ret = 0;
int curMax = 0;
int curRch = 0;
for(int i = 0; i < nums.length; i ++)
{
if(curRch < i)
{
ret ++;
curRch = curMax;
}
curMax = Math.max(curMax, nums[i]+i);
}
return ret;
}
}

[LeetCode][Java] Jump Game II的更多相关文章

  1. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  2. [LeetCode] 45. Jump Game II 跳跃游戏 II

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

  3. [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)

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

  4. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  5. LeetCode 045 Jump Game II

    题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...

  6. Java for LeetCode 045 Jump Game II

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

  7. [LeetCode] 45. Jump Game II 解题思路

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

  8. LeetCode 45 Jump Game II(按照数组进行移动)

    题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description   给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...

  9. [LeetCode] 45. Jump Game II 跳跃游戏之二

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

随机推荐

  1. Handler和内部类的正确用法

    PS:本文摘抄自<Android高级进阶>,仅供学习使用 Android代码中涉及线程间通信的地方经常会使用Handler,典型的代码结构如下. public class HandlerA ...

  2. HTML5页面直接调用百度地图API,获取当前位置,直接导航目的地

    <!DOCTYPE html> <html lang="zh-cmn-Hans"> <meta charset="UTF-8"&g ...

  3. CCF计算机职业资格认证考试 201809-2 买菜

    以下内容过于幼稚,请大佬自觉绕道.. 题目描述: 时间限制:1.0s内存限制:256.0MB问题描述:问题描述 小H和小W来到了一条街上,两人分开买菜,他们买菜的过程可以描述为,去店里买一些菜然后去旁 ...

  4. mybatis中修改了数据,控制台显示成功,数据库没有修改

    在mybatis中遇到了修改数据时,控制台显示修改成功,但是去数据库查看并没有修改,这是因为mybatis不时自动提交事务的,所以是不会修改数据库的数据,这是我们加上一句 sqlSession.com ...

  5. 零基础入门学习Python(10)--列表:一个打了激素的数组

    前言 有时候我们需要把一些东西暂时保存起来,因为他们有着一些直接或间接的联系,我们需要把它们放在某个组或者集合中,未来可能用得上. 很多接触过编程的朋友都知道,都接触过数组这个概念,那么数组这个概念事 ...

  6. c++基础_特殊回文数

    #include <iostream> using namespace std; int main(){ int n; cin>>n; ;i<;i++){ int tem ...

  7. 字符串str.format()方法的个人整理

    引言: 字符串的内置方法大致有40来个,但是一些常用的其实就那么20几个,而且里面还有类似的用法,区分度高比如isalpha,isalnum,isdigit,还有一些无时不刻都会用到的split切分, ...

  8. 版本控制git之一 仓库管理 安装 基础

      版本控制git之一-仓库管理 git ​ 再开始这个话题之前,让我想起了一件很痛苦的事情,在我大学写毕业论文的时候,我当时的文件是这样保存的 毕业论文_初稿.doc 毕业论文_修改1.doc 毕业 ...

  9. 指定PING的网卡

    struct ifreq ifr; // 绑定在eth0上 memset( &ifr, , sizeof( struct ifreq ) ); snprintf( ifr.ifr_name, ...

  10. DFS求连通块(漫水填充法)

    G - DFS(floodfill),推荐 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I6 ...