题目描述

给出一个非负整数数组,你最初在数组第一个元素的位置

数组中的元素代表你在这个位置可以跳跃的最大长度
判断你是否能到达数组最后一个元素的位置
例如

A =[2,3,1,1,4], 返回 true.

A =[3,2,1,0,4], 返回 false.

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

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:
A =[2,3,1,1,4], returntrue.

A =[3,2,1,0,4], returnfalse.


 
示例1

输入

复制

[2,3,1,1,4]

输出

复制

true
示例2

输入

复制

[3,2,1,0,4]

输出

复制

false


class Solution {
public:
    /**
     *
     * @param A int整型一维数组
     * @param n int A数组长度
     * @return bool布尔型
     */
    bool canJump(int* A, int n) {
        // write code here
        int max=0;
        for (int i=0;i<n&&max>=i;++i)
            if (i+A[i]>max) max=i+A[i];
        return max>=n-1 ?true:false;
    }
};

leetcode95:jump game的更多相关文章

  1. [LeetCode] Frog Jump 青蛙过河

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  2. [LeetCode] Jump Game 跳跃游戏

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

  3. [LeetCode] Jump Game II 跳跃游戏之二

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

  4. Leetcode 45. Jump Game II

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

  5. Leetcode 55. Jump Game

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

  6. LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]

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

  7. Leetcode jump Game II

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

  8. Leetcode jump Game

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

  9. bug report: Jump to the invalid address stated on the next line at 0x0: ???

    gdb或者vlagrind报告: ==14569== Jump to the invalid address stated on the next line ==14569== at 0x0: ??? ...

随机推荐

  1. JDBC Java 程序从 MySQL 数据库中读取数据,并备份到 xml 文档中

    MySQL 版本:Server version: 5.7.17-log MySQL Community Server (GPL) 相关内容:JDBC Java 程序从 MySQL 数据库中读取数据,并 ...

  2. # BlackLivesMatter !

    下载 # BlackLivesMatter ! https://blacklivesmatter.com/ 黑人的生命是重要的运动资源 VueCroppie VueCroppie是一个Vue 2包装C ...

  3. 磁盘 IOPS(每秒读写次数) 的计算方法

    一.磁盘 I/O 的概念 I/O 的概念,从字义来理解就是输入输出.操作系统从上层到底层,各个层次之间均存在 I/O.比如,CPU 有 I/O,内存有 I/O, VMM 有 I/O, 底层磁盘上也有 ...

  4. Java防止文件被篡改之文件校验和

    Java防止文件被篡改之文件校验和转载:请注明出处,谢谢! 1.为什么要防止文件被篡改?  答案是显然的,为了保证版权,系统安全性等.之前公司开发一个系统,技术核心是一个科学院院士的研究成果,作为一款 ...

  5. linux centos 02

    1.PS1变量,命令提示符的修改 PS1="[\u@\h \W]\$" \u  代表 用户 @   占位符 \h  主机名 \W   工作路径的最后一位 \t  \w   工作路径 ...

  6. 【自学编程】C语言编程简单的小程序,计算长方体体积!

    计算长方体体积 有朋友会说长方体体积还不好算吗?长X宽X高.没错用计算器一下就可以出结果,编程反而麻烦些,但是我们说的是这种思维,如果复杂的重复运算的话写好程序就非常简单了. 简单运算下一个固定高度的 ...

  7. centos8平台使用loginctl管理登录用户与session

    一,loginctl的用途: 控制 systemd 登录管理器 管理当前登录的用户和session 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/a ...

  8. visio2016激活 试用版

    输入秘钥: W9WC2-JN9W2-H4CBV-24QR7-M4HB8   可以成功激活成试用版 有效期30天,30天之后需要你正式激活! 经本人使用完全可以激活,欢迎大家使用!   也欢迎大家支持正 ...

  9. 干掉hao123的第n+1种方法

    最近看见我小弟无精打采的样子,问他怎么了,他说所有浏览器主页被hao123流氓劫持了,嚯,这回流氓碰同行--没辙. 我说这多大点事,百毒一下,以毒攻毒.于是便有了下面的这些方法: 适合小白: 第一种: ...

  10. WebSocket的理解

    Websocket相对于无状态的HTTp协议,是在一次成功连接之后,在关闭请求之前,服务器和客户端能顺利进行信息传输.而不用像HTTP那样每一次都要告诉服务器这个请求者是谁(身份鉴别信息). 在HTT ...