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. mysql如何开启远程连接(默认未开启,即使密码正确,仍然无法访问)

    mysql如何开启远程连接 | 浏览:1846 | 更新:2015-03-11 20:19 1 2 3 4 5 6 分步阅读 百度经验:jingyan.baidu.com 大家在公司工作中,经常会遇到 ...

  2. LNMP 1.4 nginx启动脚本和配置文件

    编写Nginx启动脚本,写入下面这段,授权755 vim /etc/init.d/nginx #!/bin/bash # chkconfig: - # description: http servic ...

  3. js中substring和substr用法与区别

    String.substring( ):用于返回一个字符串的子串 用法如下:string.substring(from, to) 其中from指代要抽去的子串第一个字符在原字符串中的位置 to指代所要 ...

  4. 2016上海浦东汽车展览会C+罩杯 车模名单

    只能帮你们到这了

  5. UML在实践中的现状和一些建议

    本文是我在csdn上看到的文章,由于认识中的共鸣,摘抄至此. 原文地址:http://blog.csdn.net/vrman/article/details/280157 UML在国内不少地方获得了应 ...

  6. php5.6,curl上传的变化

    $ch = curl_init ();curl_setopt ( $ch, CURLOPT_SAFE_UPLOAD, false); //php5.6要加上这个 $fields = array(); ...

  7. Tornado之抽屉实战(1)--分析与架构

    项目模拟地址:http://dig.chouti.com/ 知识点应用: AJAX  用于偷偷发请求 原生ajax jQuery  ajax($.ajax) iframe伪造 上传文件 传统Form ...

  8. nginx显示目录下面的文件

    location / { root /data/source; index index.html index.htm; autoindex on;     #autoindex指令,显示文件目录下面的 ...

  9. MSER

    1.注释很全的分析:http://blog.csdn.net/zhaocj/article/details/40742191 2.opencv采用的mser实现方法 * 1. the gray ima ...

  10. 前端学习笔记2017.6.21-html是个什么东西

    html有两种意思,html语言和html格式 html语言是一种面向人类的计算机语言,这是啥意思?人类用html这种语言描述出一个网页的样子,浏览器解析这个语言并展示出来. html格式是一种文件格 ...