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. Spring IoC与DI(依赖注入)

    Spring Ioc 所谓控制反转,即将传统的需要代码进行的操作,交由Spring容器来做.下面以添加book为例进行比较一下: BookService.java public interface B ...

  2. Kenneth A.Lambert著的数据结构(用python语言描述)的第一章课后编程答案

    第6题:工资部门将每个支付周期的雇员信息的列表保存到一个文本文件, 每一行的格式:<last name><hourly wage><hours worked> 编写 ...

  3. 【dp】饥饿的牛

    普通dp题 题目描述 牛在饲料槽前排好了队.饲料槽依次用1到n(1 ≤ n ≤ 2000)编号.每天晚上,一头幸运的牛根据约翰的规则,吃其中一些槽里的饲料. 约翰提供b个区间的清单.一个区间是一对整数 ...

  4. js cookie 操作

    <html> <head> <meta charset="utf-8"> <title>Javascript cookie</ ...

  5. C#基础-循环语句

    while语句 int i = 1,sum=0; while (i <= 100) { sum += i; i++; } Console.WriteLine(sum); do···while语句 ...

  6. 在SCIKIT中做PCA 逆变换 -- 新旧特征转换

    PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分量,常用于高维数据的降 ...

  7. STM32F407VET6之IAR之ewarm7.80.4工程建立(基于官方固件库1.6版本)

    今天把stm32F407的工程之IAR建立完成了,特此记录下. 下载官方固件库,STM32F4xx_DSP_StdPeriph_Lib_V1.6.1,V1.8.0版本的同理.新建以下几个文件 src放 ...

  8. PAT Basic 1079

    1079 延迟的回文数(20 分) 给定一个 k+1 位的正整数 N,写成 a​k​​⋯a​1​​a​0​​ 的形式,其中对所有 i 有 0≤a​i​​<10 且 a​k​​>0.N 被称 ...

  9. Python类元编程

    类元编程是指在运行时创建或定制类.在Python中,类是一等对象,因此任何时候都可以使用函数创建新类,而无需用class关键字.类装饰器也是函数,不过能够审查.修改,甚至把被装饰的类替换成其他类.元类 ...

  10. 大数据学习——scala的wordCount小例子

    val lines=List("hello tom hello jerry","hello tom hello kitty hello china") //方法 ...