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 ...
随机推荐
- css 横线中间添加文字
demoline01.02选一个用足够了 <style> .demo_line_01 { width: 200px;/*这指的是文字的宽度*/ padding: 0 20px 0; m ...
- 160630、五句话搞定JavaScript作用域
JavaScript的作用域一直以来是前端开发中比较难以理解的知识点,对于JavaScript的作用域主要记住几句话,走遍天下都不怕. 一.“JavaScript中无块级作用域” 在Java或C# ...
- POJ 3037 Skiing(Dijkstra)
Skiing Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4668 Accepted: 1242 Special ...
- delphi xe----操作mongoDB驱动,TMongoWire(Delphi MongoDB Driver)
所有例子来自:https://github.com/stijnsanders/TMongoWire Delphi MongoDB的驱动 一个Delphi的驱动程序来访问mongoDB的服务器.用jso ...
- Powershell Get Domain Group的几种方法
Group常见属性介绍: 一.Get-ADGroup获取群组(如下例循环获取群组的发送权限) #群组的发送权限info $groups=Get-ADGroup -filter * -SearchSco ...
- Java基础语法 - 面向对象 - 局部变量
如果在一个成员方法内定义一个变量,那么这个变量就被称为局部变量. 局部变量在方法执行时被创建,在方法执行结束时被销毁.局部变量在使用时必须进行赋值操作或被初始化,否则会出现编译错误 package m ...
- 删除DOM元素 parent.removeChild(target)
p.removeChild(p.children[0]);
- MongoDB的Python客户端PyMongo(转)
原文:https://serholiu.com/python-mongodb 这几天在学习Python Web开发,于是做准备做一个博客来练练手,当然,只是练手的,博客界有WordPress这样的好玩 ...
- 我的Android进阶之旅------>Android自定义View来实现解析lrc歌词并同步滚动、上下拖动、缩放歌词的功能
前言 一LRC歌词文件简介 1什么是LRC歌词文件 2LRC歌词文件的格式 LRC歌词文件的标签类型 1标识标签 2时间标签 二解析LRC歌词 1读取出歌词文件 2解析得到的歌词内容 1表示每行歌词内 ...
- TimeQuest学习总结
1. 基本时钟约束:creat_clock 2. 生成时钟约束:creat_generated_clock 3. I/O输入输出约束:(1)纯组合逻辑:set_max_delay & set_ ...