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. python基础学习笔记——time模块

    time模块 time翻译过来就是时间,有我们其实在之前编程的时候有用到过. #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取 ...

  2. Linux下二进制文件安装MySQL

    MySQL 下载地址:https://dev.mysql.com/downloads/mysql/ 并按如下方式选择来下载安装包. 1. 设置配置文件/etc/my.cnmore /etc/my.cn ...

  3. 解决手机助手与 android sdk 的adb 冲突问题

    现象:手机助手与 sdk 内的 adb冲突,用助手与真机连接后,sdk adb 就被干掉了 突发奇想: 突然有一天想到用助手的adb来覆盖sdk内的adb,果然奏效.现在eclipse.助手.cmd窗 ...

  4. iOS WKWebView

    Webkit 是 iOS 8.0 后提供的新的框架,组件WKWebView比较UIWebView 速度更快.占用内存更少了,可支持性更多 WKWebView可通过KVO监听属性 title.estim ...

  5. 从PHP5到PHP7的注意事项——PHP7全面删除Mysql扩展支持

    PHP删除了一些函数,其中是吓人的是两个: 1. ereg_* 系列的正则函数 2. mysql_* 系列的数据库连接函数 PHP7全面删除Mysql扩展支持,原本的mysql_*系列函数将在mysq ...

  6. [adb 连接不上的原因] 汇总

    http://www.cnblogs.com/dazhao/p/6534128.html 查看android studio 的sdk路径( 点击工具栏 ?号旁边的类似下载按钮) SDK_Manager ...

  7. 【Luogu】P2331最大子矩阵(DP)

    题目链接 这题的状态转移方程真是粗鄙. f[i][j][k]表示前i行用了j个矩阵状态为k的时候的最大值. k=0:两列都不选. k=1:取左弃右. k=2:选右弃左. k=3:左右都选,但分属于两个 ...

  8. 洛谷P3588 - [POI2015]Pustynia

    Portal Description 给定一个长度为\(n(n\leq10^5)\)的正整数序列\(\{a_n\}\),每个数都在\([1,10^9]\)范围内,告诉你其中\(s\)个数,并给出\(m ...

  9. mark一下。hadoop分布式系统搭建

    用于测试,我用4台虚拟机搭建成了hadoop结构 我用了两个台式机.一个xp系统,一个win7系统.每台电脑装两个虚拟机,要不然内存就满了. 1.安装虚拟机环境 Vmware,收费产品,占内存较大. ...

  10. MySQL 中 key, primary key ,unique key,index的区别

    一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id int(11) NOT NULL auto_increment, user_n ...