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实现简单动画——生命游戏
生命游戏 生命游戏的宇宙是一个无限的,其中细胞的二维正交网格,每个细胞处于两种可能的状态之一,即*活着*或*死亡*(分别是*人口稠密*和*无人居住*).每个细胞与它的八个邻居相互作用,这八个邻居是水平 ...
- Spring Boot实现邮件服务,附常见邮箱的配置
1. pom.xml文件中引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <art ...
- Java项目下的classpath路径包括哪里
https://my.oschina.net/zjllovecode/blog/916927 classpath指的是.classpath下kind="src" 的路径
- Win7 VS2017编译Blender2.79
去年在VS2013环境编译过一次,重装系统后换了VS2017,正好刚编译完Godot3.0.2,顺手把Blender也编译了吧. 官方Windows下编译指南 https://wiki.blender ...
- php5.6,Ajax报错,Warning: Cannot modify header information - headers already sent in Unknown on line 0
php5.6ajax报错 Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be remo ...
- [转]JDBC如何进行超时设置
文档来源:https://jingyan.baidu.com/article/fc07f98922615a12ffe519ce.html 恰当的JDBC超时设置能够有效地减少服务失效的时间.本文将对数 ...
- Linux 入门视频教程
http://v.youku.com/v_show/id_XNzM4NTU0MjQ4.html?f=28697585&o=1 1.1.1 Linux系统简介-UNIX发展历史和发行版本http ...
- 处理Word文档中所有修订
打开现有文档进行编辑 若要打开现有文档,您可以将 Word类实例化,如以下 using 语句所示. 为此,您可以使用Open(String, Boolean) 方法打开具有指定 fileName 的字 ...
- $(document).on('click','.classname',function(){}); VS $('.classname').on('click',function(){});
jquery中用on来绑定事件,经常的写法有$(document).on('click','.classname',function(){});$('.classname').on('click',f ...
- git的基本使用和问题
1,填写信息git config --global user.name "用户名"git config --global user.email "邮箱" 2,创 ...