题目描述

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

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

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. c#类(class)

    类 类的定义是以关键字class开始的,后面跟类的名称,类的主题包含一个花括号里,下面是类定义的一般格式. <access specifier> class class_name { // ...

  2. 在自己电脑上查看git远程分支列表比实际云端的远程分支要多一些

    问题 最近打开一个很久没有打开过的项目,使用git branch -a查看了一下所以分支,其中有些远程分支没有什么用了 于是准备去gitlab上删除它,结果到gitlab上发现没有这些分支,猜测是自己 ...

  3. 如何免费安装正版Adobe

    现在正版的Adobe都非常的贵,如果你想不花钱又想下载正版的Adobe,那么就请花几分钟时间学习以下本篇博客,告诉你如何免费下载正版Adobe! [一定要读完,不要看到一半就以为教您下载的是付费版] ...

  4. Makefile-4-书写命令

    目录 前言 概念 Chapter 4:书写命令 4.1 显示命令 4.2 命令执行 * 4.3 命令出错 4.4 嵌套执行 make 4.5 定义命令包 参考 前言 本笔记主要记录Makefile一些 ...

  5. go 不停模拟 写日志

    package main import ( "os" "errors" "math/rand" "time" " ...

  6. git reset 与 git revert的区别?

    一,git reset的功能: 该命令修改HEAD的位置,即将HEAD指向的位置改变为之前存在的某个版本, 说明: 修改后,push到远程仓库时需要使用"git push -f"提 ...

  7. 在 Istio 中实现 Redis 集群的数据分片、读写分离和流量镜像

    Redis 是一个高性能的 key-value 存储系统,被广泛用于微服务架构中.如果我们想要使用 Redis 集群模式提供的高级特性,则需要对客户端代码进行改动,这带来了应用升级和维护的一些困难.利 ...

  8. git学习(二) git的文件状态

    git的文件状态 用于查看git的状态 git status 用于git文件的删除操作 git rm 如果只是 git rm --cache 仅删除暂存区里的文件: 如果不加--cache 会删除工作 ...

  9. python提取视频第一帧图片

    一.实现代码 # -*- coding: utf-8 -*- import cv2 from PIL import Image from io import BytesIO def tryTime(m ...

  10. Vue(学习第三部 自动化工具 [vue-cli])

    目录 Vue自动化工具(Vue-cli) 安装nide.js npm 安装Vue-sli 使用Vue-处理初始化创建起那段项目 vue项目执行流程图 单文件组件的使用 template 编写HTML代 ...