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.

Hide Tags

Array Greedy

 

 
 
 题目很简单,思路如下:
  1. 创建一个标记为last,表示A数组下标为last 前的位置都可以达到,初始化为 1.
  2. 从左遍历数组,当前位置加上可跳跃的长度A[i] 更新last。
  3. 如果last >= n ,即可以达到数组末尾,否则失败。

我写的如下:

 #include <iostream>
using namespace std; class Solution {
public:
bool canJump(int A[], int n) {
if(n<=) return true;
int last = ;
for(int i =;i<last&&i<n;i++){
last = last>i+A[i]+?last:i+A[i]+;
if(last>=n) return true;
}
return false;
}
}; int main()
{
int A[] = {,,,,};
Solution sol;
cout<<sol.canJump(A,sizeof(A)/sizeof(int))<<endl;
return ;
}
 
 另外一个思路:
    从右到左遍历数组,如果遇到0,则判断该位置左边是否存在某位置可以跨越过该0,如果不能跨越了,则返回false。
 

 #include <iostream>
using namespace std; /**class Solution {
public:
bool canJump(int A[], int n) {
if(n<=1) return true;
int last = 1;
for(int i =0;i<last&&i<n;i++){
last = last>i+A[i]+1?last:i+A[i]+1;
if(last>=n) return true;
}
return false;
}
};
*/
class Solution {
public:
bool canJump(int A[], int n) {
for(int i = n-; i >= ; i--){
if(A[i] == ){
int j;
for(j = i - ; j >=; j--){
if(A[j] > i - j) break;
}
if(j == -) return false;
}
}
return true;
}
}; int main()
{
int A[] = {,,,,};
Solution sol;
cout<<sol.canJump(A,sizeof(A)/sizeof(int))<<endl;
return ;
}
 
 
 
 
 
 
 

[LeetCode] Jump Game 数组控制的更多相关文章

  1. LeetCode:寻找数组的中心索引【668】

    LeetCode:寻找数组的中心索引[668] 题目描述 给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法. 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和 ...

  2. LeetCode:删除排序数组中的重复项||【80】

    LeetCode:删除排序数组中的重复项||[80] 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...

  3. LeetCode初级算法--数组01:只出现一次的数字

    LeetCode初级算法--数组01:只出现一次的数字 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...

  4. LeetCode初级算法--数组02:旋转数组

    LeetCode初级算法--数组02:旋转数组 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...

  5. 前端与算法 leetcode 189. 旋转数组

    目录 # 前端与算法 leetcode 189. 旋转数组 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 189. 旋转数组 题目描述 189. 旋转数组 概要 把他当做一到简单 ...

  6. 每日一道 LeetCode (14):数组加一

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  7. LeetCode二维数组中的查找

    LeetCode 二维数组中的查找 题目描述 在一个 n*m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增.请完成一个搞笑的函数,输入这样的一个二维数组和一个整数,判断数 ...

  8. [LeetCode] Jump Game 跳跃游戏

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

  9. [LeetCode] Jump Game II 跳跃游戏之二

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

随机推荐

  1. 01_10_Struts2_2.1.6版本的中文问题

    01_10_Struts2_2.1.6版本的中文问题 1. 由于Struts2_2.1.6存在bug 正常情况下在struts.xml配置如下一行 <constant name="st ...

  2. Thinkphp5 同时连接两个库

    新建api/user.php <?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/8/25 * Time: 1 ...

  3. 面向对象之元类(metaclass)

    一.前言: 要搞懂元类必须要搞清楚下面几件事: 类创建的时候,内部过程是什么样的,也就是我们定义类class 类名()的过程底层都干了些啥 类的调用即类的实例化过程的了解与分析 我们已经知道元类存在的 ...

  4. Applied Nonparametric Statistics-lec1

    参考网址: https://onlinecourses.science.psu.edu/stat464/node/2 Binomial Distribution Normal Distribution ...

  5. 线段树:CDOJ1592-An easy problem B (线段树的区间合并)

    An easy problem B Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

  6. 水题:UVa213- Message Decoding

    Message Decoding Some message encoding schemes require that an encoded message be sent in two parts. ...

  7. C++实现Behavioral - Observer模式 (转)

    转http://patmusing.blog.163.com/blog/static/13583496020101501923571/ 也称为Dependents或Publish-Subscribe模 ...

  8. python 模块相互import

    模块A中import B,而在模块B中import A.这时会怎么样呢?这个在Python列表中由RobertChen给出了详细解释,抄录如下: [A.py] from B import D clas ...

  9. Spring Boot + Mybatis 多数据源配置实现读写分离

    本文来自网易云社区 作者:王超 应用场景:项目中有一些报表统计与查询功能,对数据实时性要求不高,因此考虑对报表的统计与查询去操作slave db,减少对master的压力. 根据网上多份资料测试发现总 ...

  10. selenium - 常用元素操作

    # 3.常用元素操作 # 元素对象的获取ele = driver.find_element_by_XXX('定位表达式') # 获取元素的文本内容(返回值为元素的文本)ele.text # 获取元素的 ...