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.

Input: [2,3,1,1,4]
Output: 2
Explanation: 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.

题意:

跟之前[leetcode]55. Jump Game青蛙跳(能否跳到终点)思路一致。

不同的是,

要求计算最小步数。

思路:

一、初始化lastReachIdx, 指针i, lastReachIdx, result都为0

二、更新curReachIdx(即当前i的势力范围)

三、指针i继续扫数组,当前指针i若大于lastReachIdx

四、则更新lastReachIdx, 同时result ++。 而curReachIdx更新为当前i对应的势力范围

五、指针i继续扫数组,当前指针i 若大于lastReachIdx,

则说明又要更新lastReachIdx并且result ++了。

代码:

 public class JumpGameII {
public int jump(int[] nums) {
int result = 0;
int lastMaxIdx = 0;
int curMaxIdx = 0;
for (int i = 0; i < nums.length; ++i) {
if (i > lastMaxIdx) {
lastMaxIdx = curMaxIdx;
++result;
}
curMaxIdx = Math.max(curMaxIdx, i + nums[i]);
}
return result;
}
}

[leetcode]45. 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 跳跃游戏之二

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

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

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

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

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

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

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

  6. Leetcode 45. 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(按照数组进行移动)

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

  8. [leetcode] 45. Jump Game II(hard)

    原题 题意: 是Jump Game的衍生题(题解),题意求跳到最后一格所需最少步数,(默认所测数据永远可以跳到最后一格). 思路: 利用贪心,遍历数组,记录在每一步可跳跃到的最大区域. 1.当前步数 ...

  9. [LeetCode] 45. Jump game II ☆☆☆☆☆(跳跃游戏 2)

    https://leetcode-cn.com/problems/jump-game-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-b ...

随机推荐

  1. Linux内核分析第七次作业

    分析Linux内核创建一个新进程的过程 Linux中创建进程一共有三个函数: 1. fork,创建子进程 2. vfork,与fork类似,但是父子进程共享地址空间,而且子进程先于父进程运行. 3. ...

  2. AES对称加密和解密(转)

    AES对称加密和解密 package demo.security; import java.io.IOException; import java.io.UnsupportedEncodingExce ...

  3. Linux touch命令详解

    Linux touch命令 Linux touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间.若文件不存在,系统会建立一个新的文件. 用法: touch [-acfm][-d<日 ...

  4. docker容器内存占用 之 系统cache,docker下java的内存该如何配置

    缘起: 监控(docker stats)显示容器内存被用完了,进入容器瞅了瞅,没有发现使用内存多的进程,使用awk等工具把容器所有进程使用的内存加起来看看,距离用完还远了去了,何故? 分析: 该不会d ...

  5. java中拼接两个对象集合

    目标:  根据两个list中每条记录的某个属性是否相同来拼接. 1.首先定义一个字符串 String str = "[{\"ITEMID\":2,\"ITEMN ...

  6. vi 使用小结

    复制 1,ny 从哪行到哪行的复制,中间用逗号隔开,然后命令y. 黏贴 是在命令模式下直接按p即可 跳到n行: 命令模式直接输入数字即可 剪切:d命令 删除:x命令 跳到行首行尾:直接home或end ...

  7. CentOS7 YUM安装与配置 MySQL5.7

    原文链接:http://blog.csdn.net/xyang81/article/details/51759200 安装环境:CentOS7 64位,MySQL5.7 1.配置YUM源 在MySQL ...

  8. 04 Python数据类型

    Python 数据型1. int: 1,2,3 ....2. bool: True False3. str: 存贮少量数据 'asjkdh','工查'4. list: 列表,存贮大量数据 [1,2,3 ...

  9. Spring用了哪些设计模式

    单例:只产生一个对象,共享对象的资源: 多例:产生多个对象,对象资源没有联系:(action) 在ssm框架中 service层.dao层.controller层都是默认使用单例模式,只会产生唯一 一 ...

  10. Jquery DataTables 获取表格数据

    1.获取表格所有数据 function getTableContent(){ var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行,nTrs[i]表示第i ...