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 ...
随机推荐
- JavaScript 工作原理之十三-CSS 和 JS 动画底层原理及如何优化其性能
原文请查阅这里,本文采用知识共享署名 4.0 国际许可协议共享,BY Troland. 本系列持续更新中,Github 地址请查阅这里. 这是 JavaScript 工作原理的第十三章. 概述 正如你 ...
- 并发工具类的使用 CountDownLatch,CyclicBarrier,Semaphore,Exchanger
1.CountDownLatch 允许一个或多个线程等待直到在其他线程中执行的一组操作完成的同步辅助. A CountDownLatch用给定的计数初始化. await方法阻塞,直到由于countDo ...
- GPS同步时钟装置应用及选择
GPS同步时钟装置应用及选择 GPS是全球定位系统的简称,GPS具有全天时.全天候.高精度.定位和授时服务,GPS卫星授时成本低.安全可靠.覆盖范围广.GPS同步时钟装置,是指从GPS卫星上获取时间信 ...
- ARC中__bridge, __bridge__transfer, __bridge_retained 关系
总结于 IOS Tuturial 中 ARC两章,详细在dropbox pdf 文档. Toll-Free Bridging 当你在 Objective-C 和 Core Foundation 对象之 ...
- 【S2-053】Struts2远程命令执行漏洞(CVE-2017-12611)
继S2-052之后,Apache Struts 2再次被曝存在远程代码执行漏洞,漏洞编号S2-053,CVE编号CVE-2017-1000112. 当开发人员在Freemarker标签中使用错误的构造 ...
- 处理asp.net core连接mysql的一个异常Sequence contains more than one matching element
晚上在那里调程序,把mysql.data组件的nuget包进行了更新,前几天好好的程序,开始抛错,跟踪断点发现以下的异常: Unable to connect to any of the specif ...
- MySQL5.7 数据库的my.cnf配置
[client] port socket=/tmp/mysql.sock default-character-set=utf8 [mysql] no-auto-rehash default-chara ...
- ggplot2(6) 标度、坐标轴和图例
6.1 简介 标度控制着数据到图形属性的映射.标度将我们的数据转化为视觉上可以感知的东西:例如大小.颜色.位置和形状.标度也为我们提供了读图时所使用的工具:坐标轴和图例. 执行标度的过程分为三步:变换 ...
- python3.6 单文件爬虫 断点续存 普通版 文件续存方式
# 导入必备的包 # 本文爬取的是顶点小说中的完美世界为列.文中的aa.text,bb.text为自己创建的text文件 import requests from bs4 import Beautif ...
- C++ 线程添加互斥量
//创建互斥量 HANDLE g_hMutex = NULL; WaitForSingleObject(g_hMutex, INFINITE); ReleaseMutex(g_hMutex);