一、

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. Webpack 核心开发者 Sean Larkin 盛赞 Vue

    dev.io 近日邀请了 Webpack 核心开发者 Sean Larkin 回答开发者提问,其中几个问提比较有意思,和掘金的小伙伴们分享一下. 先上点前菜: 有一个开发者问 Sean 如何成为一个热 ...

  2. 一文快速入门Shell脚本_了解Sheel脚本基本命令

    通过代码和注释的形式,列举了shell的基础操作,快速入门.shell在线编辑器 注释 单行用#号:多行::<<' 多行注释... '.:<<a 多行注释... a.:< ...

  3. 1,Linux(CentOS)中的基本配置

    1,hostname(主机名) 查看主机名:hostname 临时修改主机名:hostname hadoop1 永久修改主机名:vi etc/sysconfig/network :  [NETWORK ...

  4. 简单的猜数字小游戏--Python

    猜数字小游戏: #coding=utf-8 import random   answer =random.randint(1,100) #生成随机数 n=int (input("Please ...

  5. golang Printf 函数有超过 10 个转义字符

    verb 描述 %d 十进制整数 %x, %o, %b 十六进制.八进制.二进制整数 %f, %g, %e 浮点数:如 3.141593, 3.141592653589793, 3.141593e+0 ...

  6. 如何理解SiamRPN++?

    如何理解SiamRPN++? 目标跟踪: 使用视频序列第一帧的图像(包括bounding box的位置),来找出目标出现在后序帧位置的一种方法. 孪生网络结构: 在进入到正式理解SiamRPN++之前 ...

  7. macOS Catalina 升级软件问题

    最近升级macOS Catalina系统,升级失败时多尝试几次就可以执行成功了,在使用过程中发现以下问题,大家谨慎升级!!! 存在软件启动不兼容,不存在已软件激活失效问题. 有道词典不兼容,启动异常 ...

  8. vuex 业务使用

    1 创建变量 cityVuex.js export default { state: { cityArr: [] }, mutations: { setCityArr (state, arr) { s ...

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

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

  10. CSRF和XSS区别和预防

    名词解释 CSRF(Cross-site request forgery)跨站请求伪造 XSS (Cross-site scripting)跨站脚本攻击,这里缩写css被前端层叠样式表(Cascadi ...