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的更多相关文章

  1. leetcode — jump-game

    /** * Source : https://oj.leetcode.com/problems/jump-game/ * * Created by lverpeng on 2017/7/17. * * ...

  2. LeetCode: JumpGame 1 and 2

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

  3. Leetcode::JumpGame

    Description: Given an array of non-negative integers, you are initially positioned at the first inde ...

  4. [Leetcode 55]跳格子JumpGame

    [题目] Given an array of non-negative integers, you are initially positioned at the first index of the ...

  5. jump-game i&&ii 能否跳出区间 贪心

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

  6. [leetcode]55.JumpGame动态规划题目:跳数游戏

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

  7. [LeetCode] Jump Game 跳跃游戏

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

  8. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  9. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

随机推荐

  1. css 横线中间添加文字

      demoline01.02选一个用足够了 <style> .demo_line_01 { width: 200px;/*这指的是文字的宽度*/ padding: 0 20px 0; m ...

  2. 160630、五句话搞定JavaScript作用域

    JavaScript的作用域一直以来是前端开发中比较难以理解的知识点,对于JavaScript的作用域主要记住几句话,走遍天下都不怕.   一.“JavaScript中无块级作用域” 在Java或C# ...

  3. POJ 3037 Skiing(Dijkstra)

    Skiing Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4668   Accepted: 1242   Special ...

  4. delphi xe----操作mongoDB驱动,TMongoWire(Delphi MongoDB Driver)

    所有例子来自:https://github.com/stijnsanders/TMongoWire Delphi MongoDB的驱动 一个Delphi的驱动程序来访问mongoDB的服务器.用jso ...

  5. Powershell Get Domain Group的几种方法

    Group常见属性介绍: 一.Get-ADGroup获取群组(如下例循环获取群组的发送权限) #群组的发送权限info $groups=Get-ADGroup -filter * -SearchSco ...

  6. Java基础语法 - 面向对象 - 局部变量

    如果在一个成员方法内定义一个变量,那么这个变量就被称为局部变量. 局部变量在方法执行时被创建,在方法执行结束时被销毁.局部变量在使用时必须进行赋值操作或被初始化,否则会出现编译错误 package m ...

  7. 删除DOM元素 parent.removeChild(target)

    p.removeChild(p.children[0]);

  8. MongoDB的Python客户端PyMongo(转)

    原文:https://serholiu.com/python-mongodb 这几天在学习Python Web开发,于是做准备做一个博客来练练手,当然,只是练手的,博客界有WordPress这样的好玩 ...

  9. 我的Android进阶之旅------>Android自定义View来实现解析lrc歌词并同步滚动、上下拖动、缩放歌词的功能

    前言 一LRC歌词文件简介 1什么是LRC歌词文件 2LRC歌词文件的格式 LRC歌词文件的标签类型 1标识标签 2时间标签 二解析LRC歌词 1读取出歌词文件 2解析得到的歌词内容 1表示每行歌词内 ...

  10. TimeQuest学习总结

    1. 基本时钟约束:creat_clock 2. 生成时钟约束:creat_generated_clock 3. I/O输入输出约束:(1)纯组合逻辑:set_max_delay & set_ ...