一、

1、

#include<iostream>
#include<cmath>
using namespace std; bool CanJump(int n[],int num)
{
if (num==1)
return 1; //如果向量长度为 1,则
int loc;
int gla=0;
for(int i=0;i<num-1;i++)
{
if(gla<i){ //进入不到下一步
break;
}
loc=i+n[i]; //局部变量,每一个位置能达到的最远位置
gla=max(gla,loc); //全局变量,达到的最大位置 ,一定要注意判断,两者融合的过程
}
if(gla>=num-1)
{
return 1;
}
else{
return 0;
}
} int main()
{
int a[]={2,3,1,1,4};
if(CanJump(a,5)==1)
{
cout<<"True";
}
else{
cout<<"False";
}
return 0;
}

2、

当{3,2,1,0,4}时,如下结果

二、

1、在上面的基础上改

#include<iostream>
#include<cmath>
using namespace std; int CanJumpNum(int n[],int num)
{
if (num==1)
return 0; //如果向量长度为 1,则留在原地
int loc;
int gla=0;
int step=0;
for(int i=0;i<num-1;i++)
{
if(gla<i){ //进入不到下一步
break;
}
loc=i+n[i]; //局部变量,每一个位置能达到的最远位置 if(loc>gla){
step++;
} gla=max(gla,loc); //全局变量,达到的最大位置 ,一定要注意判断,两者融合的过程 if(gla>=num-1) //结束的标志
{
break;
}
}
if(gla>=num-1)
{
return step;
}
else{
return -1;
}
} int main()
{
int a[]={7,0,9,6,9,6,1,7,9,0,1,2,9,0,3};
cout<<CanJumpNum(a,15);
return 0;
}//2,3,1,1,4

出现这种情况,这是因为,两步过程是7—7—3;

而程序中的4步是。这是因为我们到7后又重头考虑,没有在7上继续加。所以这个思路是有漏洞的。

2、

#include<iostream>
#include<cmath>
using namespace std; int CanJumpNum(int A[],int n)
{
int curReach=0,maxReach = 0,steps=0;
for(int i=0;i<n && i<=maxReach;i++)
{
if(i>curReach) //steps-1步能够到达的距离,必须再走一步了
{
++steps;
curReach = maxReach;
}
maxReach = max(maxReach,i+A[i]); // step步最远距离
}
if(maxReach<n-1)
return -1;
else
return steps;
} int main()
{
int a[]={7,0,9,6,9,6,1,7,9,0,1,2,9,0,3};
cout<<CanJumpNum(a,15);
return 0;
}//2,3,1,1,4

  

  

LeetCode(一) jump game的更多相关文章

  1. Java for LeetCode 055 Jump Game

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

  2. [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

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

  3. [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)

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

  4. [LeetCode] 45. Jump Game II 跳跃游戏 II

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

  5. [LeetCode] Frog Jump 青蛙过河

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

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

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

  7. Leetcode: Frog Jump

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  8. [LeetCode] 45. Jump Game II 解题思路

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

  9. LeetCode 55. Jump Game (跳跃游戏)

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

  10. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

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

随机推荐

  1. vue-router03 vue-cli

    1.钩子: next讲解: next()进行路由跳转 next(false)取消路由跳转 beforeRouteEnter (to, from, next) { next(vm => { // ...

  2. HBuilder-X 关闭eslint-vue 插件语法检查

    HBuilder-X  在写vue项目的时候发现,代码在保存的时候回自动检查eslint语法,会报一大堆的红色警告! 这时候就很烦人,看着不爽,看了下eslint 配置里面介绍了关闭语法检查的配置-- ...

  3. 【从零单排HBase 01】从一无所知到5分钟快速了解HBase

    最近公司正好准备投入HBase,因此做了一些基础学习准备,所以先暂时停止MySQL的更新,把HBase的学习心得跟大家分享一下,接下来一段时间都会发布HBase相关内容. 在学的过程中,发现跟MySQ ...

  4. 汇编语言-[bx]和loop指令和多个段

    5.1 [BX]和内存单元的描述 要完成描述一个内存单元,需要两种信息: 内存单元的地址: 可以用 [0] 表示一个内存单元, 0 表示单元的偏移地址,段地址默认在 ds 中: 同样也可以用 [bx] ...

  5. 智慧港口——基于二三维一体化GIS的港口可视化监管平台

    “智慧港口”是以现代化基础设施设备为基础,以云计算.大数据.物联网.移动互联网.智能控制等新一代信息技术与港口运输业务深度融合为核心,以港口运输组织服务创新为动力,以完善的体制机制.法律法规.标准规范 ...

  6. Vue Snackbar 消息条队列显示,依次动画消失的实现

    效果预览 思路 封装 Snackbar 组件: 在根路由页面下建立全局 Snackbar 控制器,统一管理 Snackbar: 通过事件通知全局 Snackbar 控制器显示消息: 实现 1. 封装 ...

  7. 1分钟,带你上手“Markdown”语法

    前言 Markdown 是一种轻量级的标记语言,有 John Gruber 以及Aaron Hillel Swartz 一起编写.Aaron Hillel Swartz 是一个极富传奇的黑客,有兴趣可 ...

  8. 聊聊order by rand()

    总结写在前面: 1. 不建议直接使用order by rand(),原因是执行代价比较大 2. 介绍了内存临时表,对于内存临时表,由于回表不需要访问磁盘,所以往往是用rowid排序,可以减少参与排序字 ...

  9. Natas19 Writeup(Session登录,常见编码,暴力破解)

    Natas19: 提示,与上一题源码类似,只是PHPSESSID不连续.随便输入username和password,抓包观察PHPSESSID,发现是输入的信息,按照id-username的格式,由a ...

  10. 【开源】使用Angular9和TypeScript开发RPG游戏

    RPG系统构造 通过对于斗罗大陆小说的游戏化过程,熟悉Angular的结构以及使用TypeScript的面向对象开发方法. 项目地址 人物 和其他RPG游戏类似,游戏里面的人物角色大致有这样的一些属性 ...