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. HDU 1232 畅通工程(Kruskal)

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  2. Exchange Powershell:ForwardingAddress&InboxRule

    查询在邮箱上设置的转发功能: Get-Mailbox -server MX01 -Filter {ForwardingAddress -like '*'} | Select-Object Name, ...

  3. IO流入门-第六章-FileReader_FileWriter复制

    利用FileReader和FileWriter进行复制粘贴 /* 文件复制粘贴 只能复制纯文本文件 2017/4/13 */ import java.io.*; public class FileRe ...

  4. MyBatis 入门(一)

    1. MyBatis 概述 MyBatis 是一个半自动化的持久层框架; 核心SQL,开发人员可以进行优化; SQL和Java编码分开,功能边界清晰,一个专注业务,一个专注数据; JDBC: SQL ...

  5. CXF 框架

    1. 搭建服务端(查询天气) // 1. 引入cxf的 jar 包; // 2. 创建 SEI 接口, 需要加入注解: @WebService @WebService public interface ...

  6. 剑指Offer——从尾到头打印链表

    题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 分析: 方法1:利用栈的性质,先从头到尾遍历链表每个节点的值存入栈中,最后一个一个出栈顺序便是从尾到头的. 方法2:直接从头到尾遍历链表存储节 ...

  7. scrapy spider

    spider 定义:在spiders文件夹中由用户自定义,继承scrapy.Spider类或其子类 Spider并没有提供什么特殊的功能. 其仅仅请求给定的 start_urls/start_requ ...

  8. git学习------>如何用git log命令来查看某个指定文件的提交历史记录

    有时候接手一份新代码时,看到某些文件的改动,但不清楚这个改动的作者和原因,想查看该文件的具体提交历史记录. 今天一个同事是这样做的,直接敲git log命令,然后再使用vim命令的搜索关键字的方法来查 ...

  9. 汉澳sinox2014没有黑屏,一个能够依靠的安全避风港

    首先汉澳sinox2014没有验证server,根本就没办法区分正版和盗版 其次汉澳sinox2014安装也没有系列号cdkey等东西,直接安装无干扰 最后汉澳sinox2014不会有黑屏这样的东西. ...

  10. ATA接口寄存器描写叙述

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/mao0514/article/details/32135815 ATA接口寄存器描写叙述   .AT ...