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. CentOS 6.6 中设置Terminal快捷键

    排版比较乱,参见 https://www.zybuluo.com/Jpz/note/144583 CentOS 6.6 中设置Terminal快捷键 Linux开发环境配置 Terminal是Cent ...

  2. vmware-workstation-11中centos-6.6安装

    排版比较乱,参见 https://www.zybuluo.com/Jpz/note/144303 VMware Workstation 11中CentOS 6.6安装 Linux开发环境配置 目录 V ...

  3. Queue及Stack

    Queue 她是一个接口,有多冢实现方式(LinkedList.ArrayDeque等) 类别 方法 入队 add.offer(优先级高) 出队 remove.poll 查询 element.peek ...

  4. c 深度剖析 3

    1 typedef 和 define 的区别 #define是简单的替换;    typedef是别名! 1 2 #define pchar char * pchar a,b;//展开后 char * ...

  5. c#部分---用结构体的题目- //请输入班级人数,输入每个人的学号,姓名,和语文分数、数学分数和英语分数(要求使用结构体)

    //请输入班级人数,输入每个人的学号,姓名,和语文分数.数学分数和英语分数(要求使用结构体), //求班级里两个语文分数是最高分的学生的所有信息:数学分数是最高分的两个学生的所有信息:英语平均分 建立 ...

  6. leetcode 95 Unique Binary Search Trees II ----- java

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

  7. console下纯字符实现的贪吃蛇

    最近简直超级无聊-- code blocks win7 64编译运行无问题,应该其他编译器也不会有问题. w:上 s:下 a:左 d:右 CS标准方向控制,AK47和M4这种高级货是没有滴-- 废话不 ...

  8. kuangbin_UnionFind C (HDU 1213)

    过程模板 扫一下一共有几棵树 输出 #include <iostream> #include <string> #include <cstdio> #include ...

  9. SqlAlchemy初探

    SqlAlchemy是Python下的一个成熟的ORM框架.下面我们对他的使用做一个简略的介绍. 0.安装 如果有pip,使用pip安装,更便捷.pip install sqlalchemy 也可以下 ...

  10. (转) Lua string 操作函数

    本文转自: http://www.cnblogs.com/newlist/p/3649388.html table.keys 返回指定表格中的所有键. 格式: keys = table.keys(表格 ...