给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离。
 
求解出从下标为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. 面试的角度诠释Java工程师(一)

    前言: 我相信每一个程序员都是为了生活而努力着的.很多人因为兴趣,从此踏上了这条‘烧脑大行动’的金桥:也有很多人因为梦想和执着,奋不顾身融入这个职业:还有很多人因为被现实逼得太无奈,不得不为自己.为家 ...

  2. js如何获取asp.net服务器端控件的值(label,textbox,dropdownlist,radiobuttonlist等)

    js如何获取asp.net服务器端控件的值(label,textbox,dropdownlist,radiobuttonlist等) 欢迎访问原稿:http://hi.baidu.com/2wixia ...

  3. 小程序实现textarea随输入的文字行数变化高度自动增加

    参考链接:https://blog.csdn.net/liuwengai/article/details/78987957 该实现方法是根据上面的链接改编为小程序的实现,代码如下: wxml: < ...

  4. 在 ubuntu 【6.06、6.10】 上安装 oracle 10.2.0.1,并打补丁 10.2.0.5

    特点: ubuntu 6.06.6.10 算是很古老的ubuntu了,其应该是基于 debian 4 的 tesing/unstable 分支.所以,毛病较多. 如何安装oracle10g? 几个技术 ...

  5. android EditText设置弹出数字输入法键盘

    <EditText      android:id="@+id/edit_digit_input"      android:layout_width="wrap_ ...

  6. 检出商品详情中的图片并替换url

    原有的批量导入是按照系统本身的功能导入商品,现在需要用接口将图片上传图片服务器 所以需要将批量导入的商品图片取出来,上传后替换掉原来的url (1)检出详情中的图片,用文件名做key private ...

  7. 【matlab】=size(img)的其中两种用法&zeros( )

    i1=imread('D:\Work\1.png'); i1=rgb2gray(i1); [m,n]=size(i1); 返回图片的尺寸信息, 并存储在m.n中.其中m中存储的是行数,n中存储的是列数 ...

  8. Java如何取得当前程序部署的服务器的IP

    1.问题:之前使用InetAddress.getLocalHost().getHostAddress()时,在开发机测试可以得到192.168.0.18这样的IP.但部署到linux服务器以后, 这个 ...

  9. 如何破解银行O2O模式创新

    文/赵志宏 摩 根大通的买房APP,使客户可根据自己的位置选择合适的贷款经理:华道数据提供的卡惠APP,使客户可随时查询自己周围信用卡刷卡打折的商户信息:民生银 行的微信预约叫号功能,使客户根据可自己 ...

  10. 运行jsp常犯的错误

    error 未启动tomcat服务 tomcat端口是否已改动 404: 未部署web应用 运行时URL输入错误 检查文件的存放位置(存放文件的目录无法对外引用,如WEB-INF , META-INF ...