题目链接

题目大意:给一个数组,第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. 【bzoj5178】[Jsoi2011]棒棒糖 主席树

    题目描述 Coffee的世界里也是有棒棒糖卖的,Coffee买了N(1≤N≤50000)只连着的.这N只棒棒糖包裹在小塑料袋中,排成一列,相邻的两只棒棒糖的塑料袋是接起来的.为了方便,我们把棒棒糖从左 ...

  2. Day21-获取用户请求相关信息及请求头

    1. request里面还包含请求头等信息,可以打印看一下. views.py中的程序 from django.shortcuts import render,HttpResponse from dj ...

  3. 如何实现密码输入框focus状态弹出提示信息

    一.密码输入提示框样式实现 效果图如下: 源码如下: <html> <style type="text/css"> *{ padding: 0; margi ...

  4. 【纪念】NOIP2018后记——也许是一个新的起点

    如果你为了失去太阳而哭泣,那么你也将失去星星和月亮. —— 泰戈尔<飞鸟集> NOIP结束了,我挂了一道题……曾经在心中觉得怎么都不会考到的分数,就这么冷冷的出现在了我的成绩单上.的确是比 ...

  5. 【总结】Link-Cut Tree

    这是一篇关于LCT的总结 加删边的好朋友--Link Cut Tree Link-Cut Tree,LCT的全称 可以说是从树剖引出的问题 树剖可以解决静态的修改或查询树的链上信息:那如果图会不断改变 ...

  6. Codeforces711

    A ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on th ...

  7. hadoop(二)hadoop集群的搭建

    一.集群环境准备工作 1.修改主机名 在root 账户下 vi /etc/sysconfig/network   或者 sudo vi /etc/sysconfig/network 2.设置系统默认启 ...

  8. Java Socket TCP编程

    package com; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * Socket Se ...

  9. jsp 的 7 个动作指令

    动作指令与编译指令不同,编译指令是通知 Servlet 引擎的处理消息,而动作指令只是运行时的动作.编译指令在将 JSP 编译成 Servlet 时起作用:而处理指令通常可替换成 JSP 脚本,它只是 ...

  10. 新式类 VS 经典类

    一.概述 Python中支持多继承,也就是一个子类可以继承多个父类/基类.当一个调用一个自身没有定义的属性时,它是按照何种顺序去父类中寻找的呢?尤其是当众多父类中都包含有同名的属性,这就涉及到新式类 ...