给定一个非负整数数组,您最初位于数组的第一个索引处。
数组中的每个元素表示您在该位置的最大跳跃长度。
确定是否能够到达最后一个索引。
示例:
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. android自定义控件(二) 入门,继承View

    转载请注明地址:http://blog.csdn.net/ethan_xue/article/details/7313788 ps: 可根据apidemo里LableView,list4,list6学 ...

  2. Django:locals()小技巧

    locals()返回一个包含当前作用域里面的所有变量和它们的值的字典. 所以可以把views改写为 def current_datetime(request):     current_date = ...

  3. appium 特殊操作

      一.触摸操作   1.driver.tap([坐标],持续点击时间) 除了定位到元素的点击外,也可以通过tab实现坐标的点击 driver.tap(driver.tap([(216,1776)], ...

  4. zabbix告警邮件美化

    为了更好的用户体验,我们需要尽量美化我们的输出内容,尽量做到整齐划一,让人看了会有很舒服的感觉, 这个好像和苹果的产品一样,给人一种美感让人感觉非常享受. 一般我们的zabbix告警邮件就是纯文字,建 ...

  5. AtCoder Grand Contest #026 C - String Coloring

    Time Limit: 3 sec / Memory Limit: 1024 MB Score : 600600 points Problem Statement You are given a st ...

  6. 误删除$ORACLE_HOME/dbs下的参数文件、密码文件,如何快速重建

    [oracle@11g dbs]$ pwd/home/oracle/app/oracle/product/11.2.0/dbhome_1/dbs[oracle@11g dbs]$ lltotal 24 ...

  7. Linux User

    1.用户的工作目录,在/etc/passwd中查看 2.如果shell=bin/false(正常为bin/bash)代表禁止登录,这样就无法登录以及通过su进行切换: 3.修改,usermod -d ...

  8. python--面向对象(最全讲解)

    http://www.cnblogs.com/Eva-J/articles/7293890.html 阅读目录 楔子 面向过程vs面向对象 初识面向对象 类的相关知识 对象的相关知识 对象之间的交互 ...

  9. shell中利用ftp 上传文件夹功能

    我们知道ftp 只能用来上传或者下载文件,一次单个或者多个,怎么实现将文件夹的上传和下载呢? 可以利用先在remote ip上建立一个相同的文件夹目录,然后将文件放到各自的目录中去 1.循环遍历出要上 ...

  10. Django 中ORM 的使用

    一:Django 中 orm 的使用 1:手动新建一个数据库 2 :告诉Django连接哪个数据库 settings.py里配置数据库连接信息: #数据库相关的配置项 DATABASES ={ 'de ...