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

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:

A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

这道题的巧妙之处在于,每个值能够跳“值”大小步数。所以我们的思路例如以下:

1.记录到如今为止能跳到最远的距离,记为max

2.每个点能走的步数,记为step,且每往前面走一步。step--

3.推断到这个点后往后跳的步数是不是大于max,假设是就更新max,不是就继续往前走

这种话我们能够看到:假设前面这个点是零,且这个时候step步数不够你迈过去,那么就会自己主动跳出返回false。

可是假设能够一直这么走走到终点,那么返回true

package testAndfun;

public class jumpGame {
public static void main(String args[]){
int[] A = {3,0,0,0};
jumpGame jp = new jumpGame();
if(jp.canJump(A)) System.out.println("We win");
else System.out.println("we lose");
} public boolean canJump(int[] A) {
int max = 0;
int step = 1;
if(A.length<=1) return true;
if(A[0]==0&&A.length>1) return false;
for(int i=0;i<A.length;i++){ step--;
if(i+A[i]>max){
max = i+A[i];
step = A[i];
}
if(step==0 && i<A.length-1) return false; }
return true;
}
}

【Leetcode】经典的Jump Game in JAVA的更多相关文章

  1. 【一天一道LeetCode】#55. Jump Game

    一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...

  2. 【Leetcode】Evaluate Reverse Polish Notation JAVA

       一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...

  3. [leetcode] 403. Frog Jump

    https://leetcode.com/contest/5/problems/frog-jump/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...

  4. leetcode面试准备: Jump Game II

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

  5. leetcode面试准备: Jump Game

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

  6. [Leetcode][048] Rotate Image 略详细 (Java)

    题目在这里 https://leetcode.com/problems/rotate-image/ [个人分析] 这个题目,我觉得就是考察基本功.考察细心的,算法方面没有太多东西,但是对于坐标的使用有 ...

  7. [Leetcode][Python]55: Jump Game

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...

  8. [Leetcode][Python]45: Jump Game II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...

  9. LeetCode OJ 45. Jump Game II

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

随机推荐

  1. bash文本查看及处理工具

    文本查看及处理工具:     wc [OPTION] FILE...         -c: 字节数         -l:行数         -w: 单词数             who | w ...

  2. 自学入门 Python 优质中文资源索引

    所有资源基于 Python3 版本,全部中文内容,适用于 爬虫 / Web / 数据 方向,每个单元根据学习习惯从 书籍 / 文档 / 视频 中选择一类即可,建议任选一本书籍,然后配合文档类进行学习. ...

  3. Java-将字符串转为数字

    package com.tj; public class MyClass implements Cloneable { public static void main(String[] args) { ...

  4. Codeforces Round #416 (Div. 2) 本来以为这个时间是晚上的,下午就没做

    A. Vladik and Courtesy time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. PAT天梯赛练习题——L3-003. 社交集群(并查集按秩合并)

    L3-003. 社交集群 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在社交网络平台注册时,用户通常会输入自己的兴趣爱好, ...

  6. 【区间更新区间求和】HDU 1698 Just a Hook

    acm.hdu.edu.cn/showproblem.php?pid=1698 [AC] #include<cstdio> ; #define lson (i<<1) #def ...

  7. Spoj-BLMIRINA Archery Training

    Mirana is an archer with superpower. Every arrow she shoots will get stronger the further it travels ...

  8. 使用腾讯互动直播 遇到的坑 'GLIBC_2.14' not found 问题解决

    第一.查看系统glibc版本库 strings /lib64/libc.so.6 |grep GLIBC_ 这里我们可以看到系统中最新的版本是2.12,这里我们升级2.14. 第二.下载和安装glib ...

  9. Xen虚拟化

    Xen虚拟化基础 Xen虚拟化类型 hypervisor Xen组件 Xen hypervisor Colletion CPU.Memory.Interrupter Domain0 ---> D ...

  10. Poi写文件时报java.io.IOException: Read error

    使用POI报表时不停的报java.io.IOException: Read error,看网上是说关闭了InputStream所导致的,由于我的InputStream是读取和写入都是用的同一个,所以就 ...