给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离。
 
求解出从下标为0开始到下标到数组最后一个所需要的最少跳动次数!
 
1、当数组为空或者数组长度等于1时,不需要跳动,因此返回0 否则初始化step=1
2、初始化left=0 right = nums[0] 当left<=right时进入循环体中。 
3、从第i=0开始,如果当前跳动距离大于数组长度则返回step
4、从left开始并且初始化为0 进行遍历,区间为[0 ,  right]  不断更新max的数值  max = Math.max( max ,  left + nums[left]) 
注意上面是left+nums[left]  这里直接表示将该位置假设移动到i==0下标时 ,相对于i==0能够向前移动的距离
5、如果max的数值大于当前的right值,则更新left=right ; right=max ;step++
6、判断left和right的大小关系。重复上述的3、4、5操作
 
参考代码: 
package leetcode_50;

/***
*
* @author pengfei_zheng
* 给定数组,找出最小跳动选择
*/
public class Solution45 {
public int jump(int[] nums) {
if (nums == null || nums.length < 2) {
return 0;
}
int left = 0;
int right = nums[0];
int step = 1;
while (left <= right) {
if (right >= nums.length - 1) {
return step;
}
int max = Integer.MIN_VALUE;
for (; left <= right; left++) {
max = Math.max(max, left + nums[left]);
}
if (max > right) {
left = right;
right = max;
step++;
}
}
return -1;
}
}

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(hard)

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

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

  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. Request获取用户真实IP(用作白名单过滤)

    在Servlet里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid,Nginx等反向代理软件就不能 ...

  2. CentOS 7 64位更换内核安装锐速破解版

    1.更新内核 rpm -ivh http://soft.91yun.org/ISO/Linux/CentOS/kernel/kernel-3.10.0-229.1.2.el7.x86_64.rpm - ...

  3. Linux服务器 java生成的图片验证码乱码问题

    问题:如图所示项目中生成的图形验证码不能正常显示出需要的字体 原因:  linux下没有对应的字体 查找项目中使用到系统字体的地方,如下: 解决: 1. 在本地 路径 C:\Windows\Fonts ...

  4. 史上最全的 Sublime Text 汉化、插件安装合集

    0.前言 本文用于给新手小白用户指明前进方向.不用做商业推广. 其次,鼓舞购买正版.拒绝盗版. 好了.口号喊完,接下来就直接開始正文. 1. Sublime Text 介绍 首先在開始前,先来介绍一下 ...

  5. spark1.4配置安装

    https://segmentfault.com/a/1190000004508993

  6. 解决AF3 诡异的页面显示问题

    使用AF3开发应用,发现有一个bug,在同一个view下面的不同页面切换后,这时候切换到别的view中的页面,然后再切换到上一个view下的页面,此时只要目标不是刚才前一个view中的最后显示页面就会 ...

  7. springboot使用@ControllerAdvice(二)之深入理解

    前言: 接口类项目开发时,为了便于后期查找问题,一般会拦截器或过滤器中记录每个接口请求的参数与响应值记录, 请求参数很容易从request中获取,但controller的返回值无法从response中 ...

  8. ABBYY FineReader操作技巧

    使用ABBYY FineReader OCR文字识别软件工作即快速又简单,软件自身常常可以自行处理一切工作,用户只需点击几下软件中的‘主要’按钮.不过,有时要想获得更好的质量结果,或者解决某个不寻常的 ...

  9. web front end stack web 前段技术概览

    https://github.com/unruledboy/WebFrontEndStack

  10. 理解linux 块, i节点

    https://blog.csdn.net/zdf19/article/details/54424880 https://www.cnblogs.com/hnrainll/archive/2012/0 ...