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. linux的%用法

    转自:http://blog.csdn.net/wu020708/article/details/52387473 linux (%和%%)(#和##)贪婪匹配规则 先看一个案例,提取文件名: fil ...

  2. Minecraft Forge编程入门一 “环境搭建”

    什么是Forge Minecraft Forge is a Minecraft application programming interface (API) which allows almost ...

  3. Dart基础学习03--方法的使用

    1.本文主要讲一下Dart中的方法是怎么定义的,下面先看一个简单的例子: void printNumber(num number) { print('The number is $number.'); ...

  4. c#学习笔记之使用 TableLayoutPanel 控件设置窗体布局

    使用 TableLayoutPanel 控件设置窗体布局 在 Visual Studio IDE 左侧,找到“工具箱”选项卡. 选择“工具箱”选项卡,随即将显示工具箱.(或者,在菜单栏上,依次选择“视 ...

  5. Setting IE11 with Group Policy Preferences

    一.Setting Home Page with Group Policy Preferences 1.Open the Group Policy Management Console and cre ...

  6. Drupal 8 提供REST服务实例

    drupal8 的核心模块已经支持REST服务. 这样的话使用drupal 对外提供web service 变的简单了. 测试一下d8 的webservice : extend 中的 依赖模块:全部启 ...

  7. 隐藏显示终端的光标(shell echo,linux c printf)

    https://www.cnblogs.com/niocai/archive/2011/11/11/2245727.html 一.使用shell 的 echo 命令实现. echo -ne <c ...

  8. HTTP请求 蜘蛛的 user-agent

    百度爬虫 * Baiduspider+(+http://www.baidu.com/search/spider.htm”) google爬虫 * Mozilla/5.0 (compatible; Go ...

  9. 【转】如约而至:微信自用的移动端IM网络层跨平台组件库Mars已正式开源

    网上看到关于微信官方的跨平台跨业务的终端基础组件Mars的介绍文章,转载这这里.源代码: https://github.com/Tencent/mars作者:男人链接:https://zhuanlan ...

  10. JAVA三框架工作原理是什么?

    一.struts的工作原理: 1.初始化,读取struts-config.xml.web.xml等配置文件(所有配置文件的初始化) 2.发送HTTP请求,客户端发送以.do结尾的请求 3.填充Form ...