[LeetCode][Java] Jump Game II
题目:
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.
For example:
Given array A = [2,3,1,1,4]
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.)
题意:
给定一个非负整数数组。给定的初始化位置在数组的起始位置。
数组中的每一个元素代表着你能都在此位置跳跃的最大的距离。
你的目标是用最少的跳跃数达到数组的末尾。
比方:给定A = [2,3,1,1,4]
达到数组尾部的最小的跳跃步数为2。(用1步从索引 0 到 1, 接着用3步到达结尾索引。)
算法分析:
该题思想主要是。扫描数组,以确定当前最远能覆盖的节点。放入maxreach。
然后继续扫描,直到当前的路程超过了上一次算出的覆盖范围reach,那么更新覆盖范围,同一时候更新条数,由于我们是经过了多一跳才干继续前进的。
形象地说,这个是在争取每跳最远的greedy.
* ret:眼下为止的jump数
* curRch:从A[0]进行ret次jump之后达到的最大范围
* curMax:从0~i这i+1个A元素中能达到的最大范围
* 当curRch < i,说明ret次jump已经不足以覆盖当前第i个元素。因此须要添加一次jump,使之达到
* 记录的curMax。
AC代码:
/**
* ret:眼下为止的jump数 * curRch:从A[0]进行ret次jump之后达到的最大范围 * curMax:从0~i这i+1个A元素中能达到的最大范围 * 当curRch < i,说明ret次jump已经不足以覆盖当前第i个元素,因此须要添加一次jump。使之达到 * 记录的curMax。 */
public class Solution
{
public int jump(int[] nums)
{
int ret = 0;
int curMax = 0;
int curRch = 0;
for(int i = 0; i < nums.length; i ++)
{
if(curRch < i)
{
ret ++;
curRch = curMax;
}
curMax = Math.max(curMax, nums[i]+i);
}
return ret;
}
}
[LeetCode][Java] Jump Game II的更多相关文章
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- LeetCode 045 Jump Game II
题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...
- Java for LeetCode 045 Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 45 Jump Game II(按照数组进行移动)
题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...
- [LeetCode] 45. Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- CAD参数绘制直线(com接口)
用户可以在CAD控件视区任意位置绘制直线. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE ...
- vue的[__ob__: Observer]
为什么会获取不到里面的值 因为:vue data 里面值都是有这个属性的.这是被vue接管的数据,observer是Vue核心中最重要的一个模块(个人认为),能够实现视图与数据的响应式更新,底层全凭o ...
- 从Excel读取数据,然后分析相似的数据,多线程处理(多线程比较相似的字符串,统计出相似的数量及字符串)
之前的jar包有问题,现已修改. 需要的jar包,已修改 自己去Maven中央仓库下载jar包. excel数据: 直接上代码. 程序再度优化了一遍.之后如果想再度精准,可能需要建模,最近没空继续做了 ...
- java匹配http或https的url的正则表达式20180912
package org.jimmy.autosearch20180821.test; import java.util.regex.Matcher; import java.util.regex.Pa ...
- Q币直充-迅银渠道商(php 面向对象类)
Q币直充的一个类实现 1 <?php /** * DEC : 迅银Q币直充 * User: David Wang * Time: 2018/5/24 下午1:09 */ namespace go ...
- genymotion 双击打开后 图标只显示在任务栏 无法在电脑上显示
解决办法 删除 c:/users/user/AppData/local/Genymobile 例如:C:\Users\lenovo\AppData\Local\Genymobile 删除注册表:HK ...
- 【C语言】控制台窗口图形界面编程(四):文本输出
目录 00. 目录 01. FillConsoleOutputAttribute函数 02. FillConsoleOutputCharacter函数 03. WriteConsoleOutputCh ...
- group 和 gshadow
group组文件 位置:/etc/group 作用:存放用户的分组信息 使用 /etc/group 命令查看时,得到的数据如下: 分析上图,可以得到以下结果 第1个字段:组名 默认组名与用户名名称一样 ...
- js 技巧 (四)
//下载文件 function DownURL(strRemoteURL,strLocalURL) { try { var xmlHTTP=new ActiveXObject("Micr ...
- CentOS 6, 编译安装lamp (php-fpm)
1 整体要求 php-fpm.httpd.mysql三者分别安装在三台虚拟机上: 第一台虚拟主机用于安装Mariadb,第二台虚拟主机安装php-fpm:第三台虚拟主机安装httpd.三台主机安装完之 ...