题目链接

题目大意:给一个数组,第i个位置的值表示当前可以往前走的最远距离,求从第一个位置能否顺利走到最后一个位置。例子如下:

法一(借鉴):DP,dp[i]表示从上一个位置走到当前位置时,剩余的可以往前走的距离。dp公式是:dp[i]=max(dp[i-1],nums[i-1])-1。代码如下(耗时5ms):

     public boolean canJump(int[] nums) {
//dp[i]表示从上一个位置走到当前位置i时,还剩余的可往前走的步数是多少
int[] dp = new int[nums.length];
//dp[i] = max(dp[i- 1], nums[i - 1]) - 1
for(int i = 1; i < nums.length; i++) {
dp[i] = Math.max(dp[i - 1], nums[i - 1]) - 1;
if(dp[i] < 0) {
return false;
}
}
return true;
}

法二(借鉴):贪心,每次都更新记录当前能到达的最远距离,如果最远距离小于当前到达的位置或已到达终点,则break,更新最远距离,i+nums[i]表示当前能到达的最远距离。代码如下(耗时5ms):

     public boolean canJump(int[] nums) {
int ma = 0;
for(int i = 0; i < nums.length; i++) {
//如果最远距离小于当前位置,表示当前位置不可达
if(i > ma || ma >= nums.length - 1) {
break;
}
//更新最远距离
ma = Math.max(ma, i + nums[i]);
}
return ma >= nums.length - 1;
}

55.Jump Game---dp的更多相关文章

  1. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  2. 刷题55. Jump Game

    一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...

  3. 55. Jump Game leetcode

    55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...

  4. [Leetcode][Python]55: Jump Game

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...

  5. Leetcode 55. Jump Game & 45. Jump Game II

    55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...

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

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

  7. Leetcode 55. Jump Game

    我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...

  8. [LeetCode] 55. Jump Game 解题思路

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

  9. [LC] 55. Jump Game

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

  10. 【LeetCode】55. Jump Game 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...

随机推荐

  1. java学习1-环境搭建

    1.材料准备 2.配置文档 3.验证java是否安装成功 打开cmd-->  java -version 提示以下即成功

  2. Oracle 多表关联并且批量修改

      描述:A表有 id,or_id 字段,B表有 id,code 字段   A表有  or_id 与B表的  id 关联,现要将A.or_id 替换成  B.code 数据    UPDATE  AS ...

  3. VLC for Android 编译过程

    首先,给一个VLC的官网链接:VLC-AndroidCompile 上面有编译所需要安装的插件,环境变量的配置等等信息:虽然是英语,但也挺好理解,这里就不再详述:此文主要记录我在编译的过程中遇到的一些 ...

  4. Windows用户相关操作

    获取所有用户 NET_API_STATUS NetUserEnum( LPCWSTR servername, DWORD level, DWORD filter, LPBYTE* bufptr, DW ...

  5. Hbase(一)基础知识

    一.Hbase数据库介绍 1.简介 HBase 是 BigTable 的开源 java 版本.是建立在 HDFS 之上,提供高可靠性.高性能.列存储. 可伸缩.实时读写 NoSQL 的数据库系统. N ...

  6. bzoj3938 Robot

    3938: Robot Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 336  Solved: 112[Submit][Status][Discuss ...

  7. sourceTree的下载与安装

    一. SourceTree是什么? 一个拥有可视化界面的项目版本控制的软件,适用于git项目管理,在window和mac均可使用. 二. SourceTree下载 下载地址:SourceTree官网 ...

  8. eclipse+myeclipse 使用技巧备忘

    myeclipse 导入多模块maven项目 https://blog.csdn.net/jack85986370/article/details/51371853 maven项目在eclipse的l ...

  9. Vue2.0中的路由配置

    Vue2.0较Vue1.0,路由有了较大改变.看一下Vue2.0中的路由如何配置: 步骤一: 在main.js文件中引入相关模块以及组件及实例化vue对象配置选项路由及渲染App组件 默认设置如下: ...

  10. CentOS下安装JDK1.8

    0.卸载旧版本 键入命令java-version,查询当前JDK版本 如果版本号不是想要的,键入rpm -qa|grep gcj 键入命令 yum -y remove (后接查询得到的版本),移除老版 ...