/**
* Source : https://oj.leetcode.com/problems/jump-game/
*
* Created by lverpeng on 2017/7/17.
*
* 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.
*/
public class JumpGame { /**
* max记录目前能到的最远位置,循环数组,每次max和当前i能到的最远位置作比较,如果比max大,说明能到比原来到更远的位置,更新max
* 如果max已经大于等于数组长度,则说明能到数组末尾,返回true
* 如果当前已经大于max说明当前位置已经到不了,返回false
*
* 如果上面没有返回,说明不能到数组末尾
*
* @param arr
* @return
*/
public boolean canJump (int[] arr) {
if (arr.length <= 0) {
return false;
}
int length = arr.length;
// 记录最远能到的位置
int max = 0;
for (int i = 0; i < max && i < length - 1; i++) {
if (i + arr[i] > max) {
max = i +arr[i];
}
if (max >= length - 1) {
return true;
}
}
return false;
} public static void main(String[] args) {
JumpGame jumpGame = new JumpGame();
int[] arr = new int[]{2,3,1,1,4};
int[] arr1 = new int[]{3,2,1,0,4};
int[] arr2 = new int[]{3,2,1,0,4,1};
System.out.println("true---->" + jumpGame.canJump(arr));
System.out.println("false--->" + jumpGame.canJump(arr1));
System.out.println("false--->" + jumpGame.canJump(arr2));
}
}

leetcode — jump-game的更多相关文章

  1. [LeetCode] Jump Game 跳跃游戏

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

  2. [LeetCode] Jump Game II 跳跃游戏之二

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

  3. LeetCode——Jump Game II

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

  4. [leetcode]Jump Game @ Python

    原题地址:https://oj.leetcode.com/problems/jump-game/ 题意: Given an array of non-negative integers, you ar ...

  5. LeetCode: Jump Game II 解题报告

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  6. LeetCode: Jump Game Total 解题报告

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

  7. 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways

    引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...

  8. [LeetCode] Jump Game II 贪心

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

  9. Leetcode jump Game II

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

  10. Leetcode jump Game

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

随机推荐

  1. 渗透测试的理论部分4——开放式Web应用程序安全项目

    开放式Web应用程序安全项目(Open Web Application Security Project OWASP) 定期退出Top 10 project(排名前十的安全隐患防守规则) 公开了编写安 ...

  2. Web缓存和静态化

    Web缓存和静态化 目录 Web缓存基础... 1 什么是Web缓存... 1 Web缓存的类型... 1 为何要使用Web缓存... 1 重验证... 1 更新... 2 浏览器缓存... 2 工作 ...

  3. nlp L1

    前向最大匹配: 最大匹配出的词必须保证下一个扫描不是词表中的词或词的前缀才可以结束. 正向最大匹配算法:从左到右将待分词文本中的几个连续字符与词表匹配,如果匹配上,则切分出一个词.但这里有一个问题:要 ...

  4. ABAP 图形练习(GFW_PRES_SHOW and GRAPH_2D)

    创建屏幕0100(元素清单中含定制控制CONTAINER和OK_CODE) 创建GUI状态100(功能键含BACK和EXIT用于返回和退出 ) 代码 *&------------------- ...

  5. [swarthmore cs75] Compiler 6 – Fer-de-lance

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第8次大作业. First-class function: It treats function ...

  6. Cbv源码简单分析图

    重点:cbv源码的简单分析(有后续)

  7. python之路(四)-set集合

    set集合 set是一个无序且不重复的元素集合优点:访问速度快,解决重复问题 l1 = [1,2,34,5,6,7,4,3,3,] s2 = set(l1) #可以以列表元祖等作为参数传进来,set集 ...

  8. noip第25课资料

  9. openGL-计算机图形大作业中出现的几个错误及解决

    错误一 错误现象:按动相应按键i和o无法在x轴和y轴移动camera,但按相应按键p可以在z轴移动camera. 错误原因:为了移动camera,设置了三个全局变量x.y.z,用于gluLookAt( ...

  10. 【分布式缓存系列】集群环境下Redis分布式锁的正确姿势

    一.前言 在上一篇文章中,已经介绍了基于Redis实现分布式锁的正确姿势,但是上篇文章存在一定的缺陷——它加锁只作用在一个Redis节点上,如果通过sentinel保证高可用,如果master节点由于 ...