一、

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. 组件(4):使用slot进行内容分发

    组件的作用域(一) 父组件模板的内容在父组件作用域内编译:子组件模板的内容在子组件作用域内编译. 父子组件的编译相互独立,编译时只能使用各自作用域中的属性和方法,例如,你不可以在父组件模板内,将一个指 ...

  2. java多线程基础API

    本次内容主要讲认识Java中的多线程.线程的启动与中止.yield()和join.线程优先级和守护线程. 1.Java程序天生就是多线程的 一个Java程序从main()方法开始执行,然后按照既定的代 ...

  3. openwrt 为软件包添加服务

    手动修改 rc.local 加入也可以实现自启动,缺点手动修改太麻烦,停止只能用 kill . 配置成服务最方便了,可以启用或禁用,启动,停止,重启非常方便. 在openwrt 中使用服务 servi ...

  4. ggplot2(5) 工具箱

    5.1 简介 ggplot2的图层化架构鼓励我们以一种结构化的方式来设计和构建图形.本章旨在概述可用的几何对象和统计变换,在文中逐个详述.每一节都解决一个特定的作图问题. 5.2 图层叠加的总体策略 ...

  5. 原生的ajax,get post请求需要注意的地方

    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  6. tersserorc的简单使用

    tesserocr 是 python 的一个 OCR 库,它是对 tesseract 做的一层 Python API 封装,所以他的核心是tesseract. tesseract 的安装见 https ...

  7. 将xml处理为json对象数组

    function xmlStr2js(xmlStr) { var tagNames = xmlStr.match(/<\w+>/g) tagNames = deWeightTagNames ...

  8. 【Weiss】【第03章】双链表例程

    双链表因为多了个前向指针,需要考虑的特殊因素多了一倍 所以中间插入(这儿没写)和中间删除会比较复杂. 其它倒没什么特别的,代码如下. 测试代码 #include <iostream> #i ...

  9. 我用STM32MP1做了个疫情监控平台3—疫情监控平台实现

    目录 1.前言 2.数据接口的获取 3.Qt界面的实现 4.在开发板上运行Qt程序 5.使用无线模块联网 6.代码下载 @ 1.前言 之前我使用桌面版本Qt实现了肺炎疫情监控平台:基于Qt的新冠肺炎疫 ...

  10. JSP(二)----指令,注释,内置对象

    ##  JSP 1.指令 *  作用:用于配置JSP页面,导入资源文件 *  格式: <%@  指令名称  属性名1=属性值1  属性名2=属性值2  %> <%@ page con ...