题目链接

题目大意:给一个数组,第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. 2017 ACM Arabella Collegiate Programming Contest(solved 9/13, complex 12/13)

    A.Sherlock Bones 题意: 给出长度为n的01串,问f(i,j)=f(j,k),(i<j<k)的i,j,k取值种数.其中f(i,j)表示[i,j]内1的个数, 且s[j]必须 ...

  2. 【JavaScript&jQuery】$.ajax()

    $(function(){ $('#send').click(function(){ $.ajax({ type: "GET", url: "test.json" ...

  3. NHibernate 设置主键不自增长

    1.Model 配置 [PrimaryKey(PrimaryKeyType.Assigned,"ID")] 2.使用时要手动赋值

  4. [AT2363] [agc012_c] Tautonym Puzzle

    题目链接 AtCoder:https://agc012.contest.atcoder.jp/tasks/agc012_c 洛谷:https://www.luogu.org/problemnew/sh ...

  5. Java日期格式转换

    Java时间格式转换大全 import java.text.*;import java.util.Calendar;public class VeDate {/**   * 获取现在时间   *    ...

  6. 代码收藏系列--mysql--创建数据库、数据表、函数、存储过程命令

    创建mysql数据库 CREATE DATABASE IF NOT EXISTS `database_name` DEFAULT CHARSET utf8 COLLATE utf8_general_c ...

  7. oracle 数据库记录

    /*----------------------------------------------------------------------------*/ 问题1[--------] Selec ...

  8. Canny边缘检测算法原理及其VC实现详解(一)

    转自:http://blog.csdn.net/likezhaobin/article/details/6892176 图象的边缘是指图象局部区域亮度变化显著的部分,该区域的灰度剖面一般可以看作是一个 ...

  9. TopCoder SRM420 Div1 500pt RedIsGood

    桌面上有R 张红牌和B 张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1 美元,黑牌则付出1 美元.可以随时停止翻牌,在最优策略下平均能得到多少钱. R,B ≤ 100000. 输 ...

  10. gdb打印STL和boost容器

    http://note.youdao.com/noteshare?id=b581e0db0084b6ba3011d9d27d372c91