JumpGame,JumpGame2
JumpGame:给定一个非负整数数组,开始在第一个位置,每个位置上的数字代表最大可以跳跃的步数,判断能不能跳到最后一个位置。
例如:A=[2,3,1,1,4],在位置0处,可以跳一步到位置1,位置1跳3步到位置4.
public class JumpGame {
public static boolean canJump(int[] a)
{
int reach = 0;//代表最多能到达的位置
int i = 0; //代表每次走一步能到达的位置
for(; i < a.length && i <= reach; i ++)
{
reach = Math.max(reach, i + a[i]);
}
return i == a.length;
}
public static void main(String[] args) {
int[] a = {2,3,1,1,4};
System.out.println(canJump(a));
}
}
JumpGame2:给定一个非负整数数组,开始在第一个位置,每个位置上的数字代表最大可以跳跃的步数,求跳到最后位置需要最少的跳跃次数。
例如:A=[2,3,1,1,4],在位置0处,可以跳一步到位置1,位置1跳3步到位置4.最少次数是2。用一个数组记录到达每个位置的前一个位置。
public static int canJump2(int[] a)
{
int pre[] = new int[a.length];//记录到达i的前一位置
int reach = 0;
for(int i = 0; i < a.length; i ++)
{
if(i+a[i] > reach)
{
//reach+1 - i+a[i]的前一位置是i
for(int j = reach + 1; j <= i + a[i] && j < a.length; j ++)
{
pre[j] = i;
}
reach = i + a[i];
}
}
int count = 0;
int k = a.length -1;
while(k > 0)
{
k = pre[k];
count ++;
}
return count;
}
JumpGame,JumpGame2的更多相关文章
- leetcode — jump-game
/** * Source : https://oj.leetcode.com/problems/jump-game/ * * Created by lverpeng on 2017/7/17. * * ...
- LeetCode: JumpGame 1 and 2
Title : Given an array of non-negative integers, you are initially positioned at the first index of ...
- Leetcode::JumpGame
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [Leetcode 55]跳格子JumpGame
[题目] Given an array of non-negative integers, you are initially positioned at the first index of the ...
- jump-game i&&ii 能否跳出区间 贪心
I: Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- [leetcode]55.JumpGame动态规划题目:跳数游戏
/** * Given an array of non-negative integers, you are initially positioned at the first index of th ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
随机推荐
- 《JAVA多线程编程核心技术》 笔记:第二章:对象及变量的并发访问
一.基本概念1.安全的变量和不安全的变量2.脏读的理解3.锁重入:4.锁释放5.死循环:二.synchronized 的理解:三.synchronized 同步方法3.1 同步方法不具有继承性.3.2 ...
- 【工具】SwitchHost的使用
一.问题: 更改Host后,再次启用或者关闭启动Host,Host被恢复原状.原因是修改Host的顺序顺序有问题. 二.解决步骤: 修改Host之前,先点击右下角,关闭所有Host(白色的部分在下面表 ...
- 记录--关于Jquery uploadify 不能动态传值的问题(java)
动态传值纠结多时后无效, 后得下面一番代码,依旧无效~~ 纳了几个闷,心灰意冷下 清理了 tomcat 一次 再出运行 可以了 真心纠结很久很久 无奈之下还是得 清理清理tomcat: ...
- 4.php奇葩的地方,反引号``
今天我发现我从来没打过这外符号 ` 就是键盘的左上方, 1的左边不需要组合键, 直接按下即可.... 刚开始我还一直在找没找到.....百度一下.才知道
- 亚马逊 MWS 开发者指南 漏桶算法
流量控制与令牌桶算法|James Pan's Blog https://blog.jamespan.me/2015/10/19/traffic-shaping-with-token-bucket 服 ...
- mybatis框架(三)
mybatis框架
- 贪玩ML系列之CIFAR-10调参
调参方法:网格调参 tf.layers.conv2d()中的padding参数 取值“same”,表示当filter移出边界时,给空位补0继续计算.该方法能够更多的保留图像边缘信息.当图片较小(如CI ...
- 基于vue + typescrpt +vuecli 搭建开发环境
打算学习typeScript与vue集成,先放几个链接,留着自己学习用,后续自己写使用新的~ https://segmentfault.com/a/1190000013676663 https://j ...
- 8.Query Documents-官方文档摘录
总结 1 先插入数据 db.inventory.insertMany([ { item: "journal", qty: 25, size: { h: 14, w: 21, uom ...
- MongoDB-4: 查询(二-数组、内嵌文档)
一.简介 我们上一篇介绍了db.collection.find()可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段,我们今天介绍了对数组和内嵌文档的查询操作,尤其是 ...