leetcode — jump-game
/**
* 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的更多相关文章
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode——Jump Game II
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [leetcode]Jump Game @ Python
原题地址:https://oj.leetcode.com/problems/jump-game/ 题意: Given an array of non-negative integers, you ar ...
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- LeetCode: Jump Game Total 解题报告
Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of ...
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- python生成exe文件
安装pyinstaller pyinstaller支持python2和python3 命令行安装:pip install pyinstaller pyinstaller --icon=duoguan. ...
- Exp9 Web安全基础
Exp9 Web安全基础 20154305 齐帅 一.实验要求 本实践的目标理解常用网络攻击技术的基本原理. Webgoat实践下相关实验: [目录] [第一部分 WebGoat 8.0] 1.Web ...
- 安装完Ubuntu后通过shell脚本一键安装软件
安装完Ubuntu后通过shell脚本一键安装软件 以下代码中#是单行注释 :<<! ! 是多行注释. 运行的时候需要把多行注释去掉. 比如把以下代码保存为install.sh, 那么在终 ...
- BAT:通过连接符处理判断OR的关系
使用情况说明: 适用于对某个文件夹下不同的文件夹(名称)做不同的处理,但存在需要对其中多个文件夹(名称)进行相同处理的情况 例子中的目录结构: .\1.2.3 -- 文件夹.\a.b.c -- 文件夹 ...
- LCD_FSMC
/************************************************************************** * 文件名:LCD_FSMC.h * * 编写人 ...
- 第十一章 DNS服务器管理与配置
1.如何在WindowsServer 2008 系统中使用IIS进行多站点配置与管理? 1)运行“开始”–“程序”–“管理工具”–“服务器管理器”打开: 选择“WEB服务器(IIS)”,注意:首次安装 ...
- OpenXml修改word特定内容
采用OpenXml来修改word特定内容,如下: word: OpenXml修改word之前: OpenXml修改word之后: 代码: string path = @"C:\Users\A ...
- Scala知识点汇总
Scala数组小结 1.定长数组 定长数组:指长度不可变的数组Array.第一种方式:先声明一个数组,后初始化该数组: scala> val array = new Array[Double]( ...
- dubbo 源码学习1 服务发布机制
1.源码版本:2.6.1 源码demo中采用的是xml式的发布方式,在dubbo的 DubboNamespaceHandler 中定义了Spring Framework 的扩展标签,即 <dub ...
- 有关wkwebview和UIwebview获取html中的标签方法
wkwebview方法如下: [webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id r ...