题面

这个题面挺简单的,不难理解。给定非负数组,每一个元素都可以看作是一个格子。其中每一个元素值都代表当前可跳跃的格子数,判断是否可以到达最后的格子。

样例

Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Input: [,,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
  jump length is 0, which makes it impossible to reach the last index.

算法

只要存在一条路径可以到达最后就说明可以。我们可以从后往前看,只要前面存在元素索引加上其元素值大于目标元素索引值,就代表从前面格子可以跳到目标格子。只要我们从后往前判断满足该条件就向前滑动,即目标格子更新成为前面的格子,直到数组头则代表都能走通;否则,走不通。

倒叙遍历数组,定义target为数组尾,判断索引值+元素值 > target ?target = 该索引 :不做处理;

便利结束,判断 target = 0 ? true : false

评价

时间复杂度:O(n) 只需要遍历一次数组。

空间复杂度:O(1)

源码

 class Solution {
public:
bool canJump(vector<int>& nums) {
int len = nums.size();
if(len <= )
return true; int target = len -;
for(int i = target-; i >= ; i--)
{
if(nums[i] + i >= target)
target = i;
} return target == ;
}
};

leetcode-55. Jump Game · Array的更多相关文章

  1. 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 ...

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

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

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

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

  4. Jump Game 的三种思路 - leetcode 55. Jump Game

    Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...

  5. LeetCode 55. Jump Game (跳跃游戏)

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

  6. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    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. [leetcode]55. Jump Game青蛙跳(能否跳到终点)

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

  10. 55. Jump Game (Array; Greedy)

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

随机推荐

  1. mysql通过binlog恢复删除数据

    删除误操作有时会意外出现,如果你有备份表数据的好习惯,那么至少你可以追回备份前的那些数据.如果我们打开了mysql的binlog,那么可以通过它的增量操作日志来恢复数据.怎么打开binlog前篇已有说 ...

  2. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_08-SpringSecurityOauth2研究-解决swagger-ui无法访问

    3.3.4.4 解决swagger-ui无法访问 当课程管理加了授权之后再访问swagger-ui则报错: 这里默认配置的了所有的请求都必须认证 把图片认证的路径加进去的话 那么访问课程图片的列表 就 ...

  3. Github排名靠前的iOS库

    //iOS第三方开源库的吐槽和备忘 http://blog.ibireme.com/2013/09/23/ios-third-party-libs/#more-41361 //整理了一份Github上 ...

  4. 【miscellaneous】MPEG2、MPEG4、H264的差异

    MPEG2.MPEG4.H264的差异 MPEG-2简介 MPEG-2制定于1994年,设计目标是高级工业标准的图象质量以及更高的传输率.MPEG-2所能提供的传输率在3-10Mbits/sec间,其 ...

  5. rebbitMQwindows安装及使用

    python中RabbitMQ的使用(安装和简单教程) 1,简介 RabbitMQ(Rabbit Message Queue)是流行的开源消息队列系统,用erlang语言开发.   1.1关键词说明: ...

  6. JavaSE基础(二)--Java环境配置

    Java 开发环境配置 在本章节中我们将为大家介绍如何搭建Java开发环境. Windows 上安装开发环境 Linux 上安装开发环境 安装 Eclipse 运行 Java window系统安装ja ...

  7. Error Retries and Exponential Backoff in AWS

    Error Retries and Exponential Backoff in AWS https://docs.aws.amazon.com/general/latest/gr/api-retri ...

  8. sql server 日期函数

    一.sql server日期时间函数Sql Server中的日期与时间函数 1.  当前系统日期.时间     select getdate()  2. dateadd  在向指定日期加上一段时间的基 ...

  9. Centos7.0配置Hadoop2.7.0伪分布式

    一.ssh免密登录 1.命令ssh-keygen. overwrite输入y一路回车 2.将生成的密钥发送到本机 ssh-copy-id localhost中间会询问是否继续输入“yes” 3.测试免 ...

  10. 使用 IDEA 创建 maven 项目

    文章目录 第一步 第二步 第三步 目录结构的设置 看下 web.xml 配置文件 第一步 点击 maven : 勾选从模板创建 : 选择 webapp : 第二步 第三步 如果你自己对 maven 进 ...