题面

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

样例

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. Qt编写数据导出到Excel及Pdf和打印数据

    一.前言 用Qt开发已经九年了,期间用Qt做过不少的项目,在各种项目中有个功能很常用,尤其是涉及到数据记录存储的项目,那就是需要对查询的数据进行导出到Excel,或者导出到Pdf文件,或者直接打印查询 ...

  2. 腾讯ios内部视频,什么垃圾视频

    前几天朋友在网上花钱买了个,腾讯ios内部视频,我也跟着下载了, 看着这列表,我感觉没什么东西,一看就是基础的东西,完全没有实战的内容,就像培训机构骗学生的东西啊,讲些毛理论,结果一到实战了,问个Sc ...

  3. JQuery.BlockUI使用方法举例

    JQuery.BlockUI是众多JQuery插件弹出层中的一个,它小巧(原版16k,压缩后10左右),容易使用, 功能齐全,支持Iframe,支持Modal,可定制性高也意味他默认谦虚的外表. jQ ...

  4. 【Leetcode_easy】674. Longest Continuous Increasing Subsequence

    problem 674. Longest Continuous Increasing Subsequence solution class Solution { public: int findLen ...

  5. office word 設置默認的縮放為100%, office word set default Zoom to 100% permanently

    如題,今天剛好遇到一個老師不知道什麽原因造成Microsoft office word 每次打開都會縮放到70% ,所以google 了一下,還真有解決方法. 只要將默認的 使用者範本檔 Normal ...

  6. 【VS开发】Wix 安装教程

    original link :  http://www.cnblogs.com/stoneniqiu/p/3355086.html 因为项目需要,最近在研究Wix打包部署,园子里也有一些关于wix ...

  7. 【计算机视觉】detection/region/object proposal 方法综述文章

    目录(?)[-] Papers 大纲 各种OP方法的回顾 Grouping proposal methods Window scoring proposal methods Aliternate pr ...

  8. 创建SSH密钥

    Step1: 检查SSH密钥 首先,打开终端,输入以下指令,查看是否已经生成SSH密钥. cd ~/.ssh ls # Lists the files in your .ssh directory 在 ...

  9. 解决maven打包时,会编译特定文件导致文件不可用

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resou ...

  10. React native 放大点击区域 hitSlop属性的使用

    在日常的需求中,如上图的加减按钮,可能写ui布局的时候没考虑实际的这个点击范围太小,不利于真机上用户点击到,如果加包裹层加padding的话又会影响原先定好的布局,或者不利于对齐. 那么可以用  hi ...