给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离。
 
求解出从下标为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. git解决冲突(rebase版)

    当使用git rebase碰到冲突时, git rebase <Remote Branch>/<Your Branch> 信息如下: error: Failed to merg ...

  2. win7下安装双系统Ubuntu14.04后开机没有win7,直接进入Ubuntu

    开机进入Ubuntu后,打开命令端,输入: sudo update-grub 然后重启,则解决问题

  3. springboot-mybatis-plus基本项目框架

    此仅仅为web最基本框架, 统一异常管理.接口统一日志管理. 项目结构: 注: 修改为如下图,作用是sql打印输出. 源码下载:https://files.cnblogs.com/files/007s ...

  4. iOS:获取 NSDate 的年

    NSDate *currentDate = [NSDate date]; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComp ...

  5. bash: /usr/bin/npm: No such file or directory

    一个整得很烂了的Ubuntu服务器, 各种问题乱出. npm老是升不到最新版(一直显示1.4),于是我干脆删了, 结果再去装却装不上了, 如果用apt-get install npm安装, 就得到如下 ...

  6. virtualbox谨记:续....

    接“virtualbox谨记:win7上只有4.3.x的版本支持ubuntu14.04.3虚拟机安装Oracle Rac,其他的版本3.x和5.0.2(至2015-08-30)均不可以”, 续 me自 ...

  7. 【转】ZooKeeper详细介绍和使用第一节

    一.分布式协调技术 在给大家介绍ZooKeeper之前先来给大家介绍一种技术——分布式协调技术.那么什么是分布式协调技术?那么我来告诉大家,其实分布式协调技术 主要用来解决分布式环境当中多个进程之间的 ...

  8. 安装MongoDB 到服务器

    用管理员身份运行CMD > cd C:\Program Files\mongodb\bin > C:\Program Files\mongodb\bin>mongod --dbpat ...

  9. iOS使用NSURLConnection发送同步和异步HTTP Request

    1. 同步发送 - (NSString *)sendRequestSync { // 初始化请求, 这里是变长的, 方便扩展 NSMutableURLRequest *request = [[NSMu ...

  10. Dubbo -- 系统学习 笔记 -- 示例 -- 结果缓存

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 结果缓存 结果缓存,用于加速热门数据的访问速度,Dubbo提供声明式缓存,以减少用 ...