给定一个非负整数数组,您最初位于数组的第一个索引处。
数组中的每个元素表示您在该位置的最大跳跃长度。
确定是否能够到达最后一个索引。
示例:
A = [2,3,1,1,4],返回 true。
A = [3,2,1,0,4],返回 false。
详见:https://leetcode.com/problems/jump-game/description/

Java实现:

方法一:

class Solution {
public boolean canJump(int[] nums) {
int n=nums.length;
// maxJump是维护的当前能跳到的最大位置
int maxJump=0;
for(int i=0;i<n;++i){
// i>maxJump表示无法到达i的位置,失败
// maxJump >= (n - 1),此时的距离已经足够到达终点,成功
if(i>maxJump||maxJump>=(n-1)){
break;
}
// nums[i]+i当前跳最远距离 maxJump为i之前跳最远距离
maxJump=maxJump>(i+nums[i])?maxJump:(i+nums[i]);
}
return maxJump>=(n-1);
}
}

方法二:

class Solution {
public boolean canJump(int[] nums) {
int n=nums.length;
// dp[i]表示当前跳跃的最大距离
int[] dp=new int[n];
dp[0]=nums[0];
// i表示当前距离,也是下标
for(int i=1;i<n;++i){
// i点可达
if(i<=dp[i-1]){
dp[i]=dp[i-1]>(nums[i]+i)?dp[i-1]:(nums[i]+i);
}else{
dp[i]=dp[i-1];
}
}
return dp[n-1]>=(n-1);
}
}

参考:https://blog.csdn.net/mine_song/article/details/69791029

055 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(跳跃游戏)

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

  3. [LeetCode] 55. Jump Game 跳跃游戏

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

  4. Leetcode55. Jump Game跳跃游戏

    给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true ...

  5. [LeetCode] 45. Jump Game II 跳跃游戏 II

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

  6. LeetCode(45): 跳跃游戏 II

    Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...

  7. 【LeetCode每天一题】Jump Game II(跳跃游戏II)

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

  8. 跳跃游戏 12 · Jump Game 12

    跳跃游戏 1 [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: 由于要用itera ...

  9. lintcode: 跳跃游戏 II

    跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A =  ...

随机推荐

  1. SocketSessionConfig参数设制

    Mina 是一个韩国人写的基本java NIO的一个高性能的传输框架,我们的搜索就是基本它作为一个搜索服务开放接口了.对于系统的TIME_WAIT过多,造成服务器的负载过高,这个问题我也不用多说了,这 ...

  2. openfire性能调优

    1. 参考 http://blog.csdn.net/foxisme2/article/details/7521139 http://blog.csdn.net/foxisme2/article/de ...

  3. OGG 11g Checkpoint 详解

    OGG Checkpoint 详解   定位中断的位置,下次启动从中断的位置开始恢复.     1.target  端配置: 2.一条记录对应一个replicat   一. Extract Check ...

  4. border-radius实现圆弧阴影效果

    1 原理 利用border-radius实现一个圆弧边的矩形,并添加box-shadow,然后放在目标元素的下方 demo: html <div class="demo1"& ...

  5. json格式化插件

    插件名称:JSON-Handle 下载地址: http://jsonhandle.sinaapp.com/

  6. GET请求中的乱码原理解析和解决方案

    2. 乱码问题解决 基础知识 1)浏览器会在中文的UTF-8后加上上%得到URL编码   例如: %e8%b4%b9%e7%94%a8%e6%8a%a5%e9%94%80 2)以get的请求发送到to ...

  7. 关于System类中out属性 实例化的问题

    System类中out属性的声明是这样的: public final static PrintStream out = nullPrintStream(); private static PrintS ...

  8. Spring入门第十八课

    Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或者基于XML配置的AOP 看代码: package lo ...

  9. ACM-ICPC2018徐州网络赛 Features Track(二维map+01滚动)

    Features Track 31.32% 1000ms 262144K   Morgana is learning computer vision, and he likes cats, too. ...

  10. 根据xml文件自动生成xsd文件

    根据xml生成xsd文档 1. 找到vs自带的xsd.exe工具所在的文件夹位置: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin 注意 ...