LeetCode(一) jump game
一、

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的更多相关文章
- Java for LeetCode 055 Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [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 ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [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 ...
- Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
- 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 ...
- [LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 55. Jump Game (跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [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 ...
随机推荐
- npm注册源的配置
npm注册源的更换 在墙内久了,难免会碰到撞墙的时候,所幸国内也有众多 NPM 镜像可供选择,在大多数情况下我们可以使用国内的源(比如 淘宝 NPM 镜像)去替换官方的源以加快下载包的速度.不过呢,我 ...
- python数据分析工具 | pandas
pandas是python下强大的数据分析和探索工具,是的python在处理数据时非常快速.简单.它是构建在numpy之上的,包含丰富的数据处理函数,支持时间序列分析功能,支持灵活处理缺失数据. pa ...
- 通过filebeat、logstash、rsyslog采集nginx日志的几种方式
由于nginx功能强大,性能突出,越来越多的web应用采用nginx作为http和反向代理的web服务器.而nginx的访问日志不管是做用户行为分析还是安全分析都是非常重要的数据源之一.如何有效便捷的 ...
- 2020最新ArchLinux安装(KDE桌面)
许多网友反映之前的教程安装好后连不上互联网,最近我刚好又安装了一遍,总结出以下没毛病的过程 按照此教程需要你会基本的vim操作(或其他文本编辑工具比如nano),基本的fdisk分盘操作(或其他分盘工 ...
- ggplot2(10) 减少重复性工作
10.1 简介 灵活性和鲁棒性的敌人是:重复! 10.2 迭代 last_plot()用于获取最后一次绘制或修改的图形. 10.3 绘图模板 gradient_rb <- scale_colou ...
- Spark入门(四)--Spark的map、flatMap、mapToPair
spark的RDD操作 在上一节Spark经典的单词统计中,了解了几个RDD操作,包括flatMap,map,reduceByKey,以及后面简化的方案,countByValue.那么这一节将介绍更多 ...
- Java安装和配置
一. Java安装和配置 1.JDK下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-21331 ...
- turtle学习笔记续集
turtle(海龟)是Python重要的标准库之一,它能够进行基本的图形绘制.turtle图形绘制的概念诞生于1969年,成功应用于LOGO编程语言. turtle库绘制图形有一个基本框架:一个小海龟 ...
- Java基础(五):数组
数组,一种应用非常广泛的数据结构,简单地来说就是一组类型相同且无序的元素的存储在固定长度且有序的内存空间. 创建一个数组 在Java中,我们可以通过[]去声明一个指定类型的数组 int[] a; // ...
- javascript中事件概述
事件就是用户或浏览器自身执行的某种动作.诸如click.load.和mouseover,都是事件的名字.而响应某个事件的函数就叫做事件处理程序(或事件侦听器).事件处理程序的名字以"on&q ...