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.

===========

非负数组,数组元素表示在当前位置能jump的最大距离,

问:是否能到达最后的位置?

----------

思路:正向贪心的思路,

每一步记住能够到达最远的距离,就好。

=====

code

class Solution {
//本题正向贪心
public:
bool canJump(vector<int>& nums) {
int maxLocation;//当前可能到达的最大位置(下标)
maxLocation = nums[];
int length = nums.size();
for(int i = ;i<length && maxLocation>=i;i++){
          ///maxLocation>=i 在这里是剪枝,遇到1,2,0.0.0.0.0.0.0....这样直接返回,无需遍历整个数组了。
maxLocation = max(maxLocation,i+nums[i]);
}
return maxLocation >= (length-);
}
};

2,也可以采用爬楼梯方法

思路:

@int max_left
bool canJump{
max_left = nums.size()-;
for(int i = nums.size()-;i>=;i--){
if(nums[i]+i>=max_left) max_left = i;
}
return max_left==?
}

55. Jump Game的更多相关文章

  1. 55. Jump Game leetcode

    55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...

  2. [Leetcode][Python]55: Jump Game

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...

  3. 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 ...

  4. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  5. 刷题55. Jump Game

    一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...

  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

    我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...

  8. Jump Game 的三种思路 - leetcode 55. Jump Game

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

  9. [LeetCode] 55. Jump Game 解题思路

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

随机推荐

  1. Codeforces Round #366 (Div. 2) A

    A. Hulk time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  2. MySQL 参数autoReconnect=true 解决8小时连接失效

    <!--  dataSource加参数    处理mysql 8小时自动断开连接的问题 --> <property name="testWhileIdle" va ...

  3. 不容易系列之(3)—— LELE的RPG难题

    有排成一行的n个方格,用红(Red).粉(Pink).绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.求全部的满足要求的涂法. 思路:运用递归算法. a[1 ...

  4. Codeforces Round #137 (Div. 2)

    A. Shooshuns and Sequence 显然\([k,n]\)之间所有数均要相同,为了求最少步数,即最多模拟\(n\)次操作即可. B. Cosmic Tables 映射\(x_i,y_i ...

  5. Unity. Navigation和寻路

    Navigation Static:不会移动.可以用于计算可行走区域.例如:地板.墙.静态障碍物. 将一个物体选为Navigation Static:Navigation窗口-> 勾选项

  6. A quest for the full InnoDB status

    When running InnoDB you are able to dig into the engine internals, look at various gauges and counte ...

  7. SQL Server中数据库文件的存放方式,文件和文件组

    原文地址:http://www.cnblogs.com/CareySon/archive/2011/12/26/2301597.html   SQL Server中数据库文件的存放方式,文件和文件组 ...

  8. ISV 和SI 是什么

    ISV是Independent Software Vendors 的英文缩写,意为"独立软件开发商",特指专门从事软件的开发.生产.销售和服务的企业,如微软(Microsoft). ...

  9. linux node&& npm 安装方式

    1.  编译好的文件 简单说就是解压后,在bin文件夹中已经存在node以及npm,如果你进入到对应文件的中执行命令行一点问题都没有,不过不是全局的,所以将这个设置为全局就好了. cd node-v0 ...

  10. 当当网开源Dubbox,扩展Dubbo服务框架支持REST风格远程调用

    当当网近日开源了Dubbox项目,可为Dubbo服务框架提供多项扩展功能,包括REST风格远程调用.Kryo/FST序列化等等. 当当网架构部和技术委员会架构师沈理向InfoQ中文站介绍了Dubbox ...