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.

 class Solution {
public:
bool canJump(int A[], int n) {
int reach = ;
for (int i = ; i <= reach && i < n; ++i) {
reach = max(reach, A[i] + i);
if (reach >= n - ) {
return true;
}
}
return reach >= n - ;
}
};

用一个reach变量保存可以达到的最远位置,从前向后扫描不断地扩大reach的范围,若reach可以达到目标位置则返回true.

【Leetcode】Jump Game的更多相关文章

  1. 【LeetCode】Jump Game (一维动态规划 + 线性扫描)

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

  2. 【leetcode】Jump Game I & II (hard)

    Jump Game (middle) Given an array of non-negative integers, you are initially positioned at the firs ...

  3. 【leetcode】 Jump Game

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

  4. 【leetcode】Jump Game I, II 跳跃游戏一和二

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

  5. 【LeetCode】Jump Game II

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

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. pl/sql使用部分整理

    在工作中使用pl/sql工具,总结一下常用操作,以便以后复习,也希望帮助朋友们熟悉pl/sql操作! 1.pl/sql记住登录密码PL/SQL Developer->tools->Pref ...

  2. js-tree坑

    今天遇到一个js坑,一个页面,有两棵树,用同一个套参数初始化的,,,,当选择完另一个棵树之后,再操作另一颗树,不选择树节点,就会有错误出现,,,

  3. oracle --(二)分区(extent)

    基本关系:数据库---表空间---数据段---分区---数据块 一.分区(extent)分区extent是比数据块大一级的存储结构,是几个逻辑上相邻的data block的组合.我们知道,物理存储通常 ...

  4. lucene、solr中的日期衰减方法-------function query --尚未测试在solr4.8

    经常有一种情景是这样的:我们索引了N年的文章,而查询时候无论直接用相关度.或者用时间排序,都是比较鲁莽的:我们想要一种既要相关度比较高,又要时间上比较新的文章. 这时候的解决办法就是,自定义日期衰减的 ...

  5. java中的面向对象的三大基本特征

    转载,原文来自http://blog.sina.com.cn/s/blog_5f79a56a0100c6ig.html 众所周知,java中的面向对象的三大基本特征是:[封装].[继承].[多态] 一 ...

  6. c#基础 函数

    函数  就是方法 独立完成某项功能的一个个体 函数的的三个好处:1.提高代码的重用性.2.提高功能开发的效率,3.提高代码的可维护性. 函数也分为     固定功能函数, 高度抽象函数.高度抽象函数应 ...

  7. Windows cmd 将命令(/指令)写到一个文件里,直接运行这个文件。提高工作效率

    Windows cmd 批处理(cmd/bat)文件的简单使用介绍 前言 如果你想我一样,要每天都需要在cmd上,用键盘去敲击相同的命令,时间一长,你就觉得很无聊.有没有什么比较高效的方法,让我们不用 ...

  8. Matlab 摄像机标定+畸变校正

    博客转载自:http://blog.csdn.net/Loser__Wang/article/details/51811347 本文目的在于记录如何使用MATLAB做摄像机标定,并通过opencv进行 ...

  9. html 5 data-* (dataset) 属性和 jquery data方法比较

    一些文章在介绍html 5 data-* (dataset)属性时,会提到jquery的data方法,认为data方法能够很好的利用html 5的这个特性.但实际上,二者的兼容性是很差的.下面给出一段 ...

  10. head first 设计模式 观察者模式

    Head first 设计模式:定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会受到通知并自动更新.   让主题与观察者之间松耦合 大话设计模式:定义了一种一对多的依赖关 ...