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

时间:2022-06-05 22:00:17

 
给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离。
 
求解出从下标为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&period; Jump Game II(贪心)

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

  2. &lbrack;leetcode&rsqb;45&period; Jump Game II青蛙跳&lpar;跳到终点最小步数&rpar;

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

  3. leetCode 45&period;Jump Game II &lpar;跳跃游戏&rpar; 解题思路和方法

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

  4. &lbrack;LeetCode&rsqb; 45&period; Jump Game II 跳跃游戏 II

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

  5. &lbrack;LeetCode&rsqb; 45&period; Jump Game II 跳跃游戏之二

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

  6. &lbrack;LeetCode&rsqb; 45&period; Jump Game II 解题思路

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

  7. &lbrack;leetcode&rsqb; 45&period; Jump Game II(hard)

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

  8. &lbrack;LeetCode&rsqb; 45&period; Jump game II &star;&star;&star;&star;&star;&lpar;跳跃游戏 2&rpar;

    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&period; Jump Game II

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

随机推荐

  1. ngOptions

    ngOptions select as select as label for value in array <select ng-model="myColor" ng-op ...

  2. 一个java实习生两周八次的面试经历

    以前从来没有因为求职出去面试过,一直觉得面试很可怕,没想到最近两周我也成为了面霸,两周面试八次,我的找工作之路就这样开始了!大概两个星期之前,我看着自己在招聘网站上写好的简历连投出去的勇气都没有,战战 ...

  3. VendorNPC&period;lua --随身商人

    print(">>Script: More Vendor NPC.") local NPCNAME="随身商人" --GOSSIP_ICON 菜单图 ...

  4. Android -- 自定义View小Demo,关于Path类的使用(一)

    1,在我们知道自定义view中onDraw()方法是用于绘制图形的,而Path类则是其中的一个重要的类,如下图效果: 代码也没有什么难度,直接贴出来吧 @Override protected void ...

  5. 关于MySQL中使用LOAD DATA INFILE导入csv文件时的日期格式问题

    在使用MySQL时,常常会用到Load Data Infile来导入数据,在遇到Date类型的列时,有时会遇到格式转换的问题: 首先创建一张简单的people表,包含名字,生日,年龄三个字段: mys ...

  6. ssh配置连接

    SSH构造: 1.改动vi /etc/ssh/sshd_config,依据模板将要改动的參数凝视去掉并改动參数值: Port 22 指定SSH连接的port号,安全方面不建议使用默认22port Pr ...

  7. java&colon; Multiple encodings set for module chunk test "GBK" will be used by compiler

    IDEA 进行编译代码的时候,特别是新项目 特别容易出现 编码错误,但是 File-Encoding中设置的又没有问题,而且maven 是能打包的,就是用 idea 自带的 编译的时候 就会出现提示 ...

  8. python网络编程基础

    一.客户端/服务器架构 网络中到处都应有了C/S架构,我们学习socket就是为了完成C/S架构的开发. 二.scoket与网络协议 如果想要实现网络通信我们需要对tcpip,http等很多网络知识有 ...

  9. 微信小程序-用户拒绝授权使用 wx&period;openSetting&lpar;&lbrace;&rcub;&rpar; 重新调起授权用户信息

    场景模拟:用户进入微信小程序-程序调出授权 选择拒绝之后,需要用到用户授权才能正常使用的页面,就无法正常使用了. 解决方法:在用户选择拒绝之后,弹窗提示用户 拒绝授权之后无法使用,让用户重新授权(微信 ...

  10. WARNING&colon; Configuration &&num;39&semi;compile&&num;39&semi; is obsolete and has been replaced with &&num;39&semi;implementation&&num;39&semi; and &&num;39&semi;api&&num;39&semi;&period;

    点击报错信息中的app, 按照提示,修改compile 为 implementation 再次同步即可 结果