一天一道LeetCode系列

(一)题目

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], return true.

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

(二)解题

思路可以参考这篇博文:【一天一道LeetCode】#45. Jump Game II

考虑在O(n)复杂度的情况下解决此题。用两个参数来lastmax和curmax来存储i前面能跳到的最远距离和当前能跳到的最远距离,如果此刻i还在lastmax之内,就要更新lastmax的值为max(lastmax,curmax)


/*

代码很简单,主要注意两个点:

1、i小于lastmax才能改变lastmax的值

2、是否能跳到重点取决于lastmax的值

*/

class Solution {

public:

    bool canJump(vector<int>& nums) {

        int lastmax = 0;

        int curmax = 0;

        for(int i = 0 ; i < nums.size() ; i++)

        {

            curmax = i+nums[i];

            if(i<=lastmax)

            {

                lastmax = max(lastmax,curmax);

            }

        }

        if(lastmax >=nums.size()-1) return true;

        else return false;

    }

};

【一天一道LeetCode】#55. Jump Game的更多相关文章

  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. Jump Game 的三种思路 - leetcode 55. Jump Game

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

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

    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 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_ Medium tag: Dynamic Programming

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

  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. LeetCode: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

随机推荐

  1. 动手实现一个vue中的模态对话框组件

    写在前面 对话框是很常用的组件 , 在很多地方都会用到,一般我们可以使用自带的alert来弹出对话框,但是假如是设计 出的图该怎么办呢 ,所以我们需要自己写一个对话框,并且如果有很多地方都用到,那我们 ...

  2. Java第4次实验提纲(面向对象2-继承、多态、抽象类与接口与Swing)

    PTA 题集面向对象2-进阶-多态接口内部类 第1次实验 1.1 题集5-1(Comparable) 难点:如果传入对象为null,或者传入对象的某个属性为null,怎么处理? 1.2 题集5-2(C ...

  3. Android Studio精彩案例(六)《使用一个Demo涵盖补间动画所有知识》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 元旦假期里,闲的无事,看到美团加载数据的动画,就突想写个Demo把动画知识集成一下.后来想了想,还是直接用一个Demo来把所有动画知识 ...

  4. 独立开发一个云(PaaS)的核心要素, Go, Go, Go!!!

    最近一年的工作,有很大的比重在做云平台的事情,简单来说,就是为公司内用户提供一个PaaS,用户可以在我们的云平台上方便的将单机服务程序扩展为多实例程序,以平台服务化的方式对外提供.在这里简单分享一下. ...

  5. Zookeeper动态更新服务器列表

    -------------------------------------------------------------------------------------- [版权申明:本文系作者原创 ...

  6. 2016年年终CSDN博客总结

    2015年12月1日,结束了4个月的尚观嵌入式培训生涯,经过了几轮重重面试,最终来到了伟易达集团.经过了长达3个月的试用期,正式成为了伟易达集团的助理工程师. 回顾一年来的学习,工作,生活.各种酸甜苦 ...

  7. C++ 虚函数表 多重继承

    上次研究的是单继承的情况,这次研究多重继承下的虚函数表的排列情况. 这次A,A1,A2,B这几个类的继承关系如下图: 测试代码如下: #include<iostream> using na ...

  8. Effective C++ ——模板和泛型编程

    条款41:了解隐式接口和编译器多态 以public继承的类,

  9. Dynamics CRM 自定义上传附件的图片悬浮层显示

    CRM中的附件是以流的形式保存在了数据库中,这样做的一个坏处是一旦系统运行时间久,附件上传的多了势必会导致数据库极速扩大,即影响系统的运行效率也对后期的迁移维护带来了不必要的麻烦.所以很多的客户都会要 ...

  10. 剑指Offer——CVTE校招笔试题+知识点总结(Java岗)

    剑指Offer(Java岗)--CVTE校招笔试题+知识点总结 2016.9.3 19:00参加CVTE笔试,笔试内容如下: 需要掌握的知识:Linux基本命令.网络协议.数据库.数据结构. 选择题 ...