[LeetCode]Jump GameII
题目:Jump GameII
如果要求找最小的调数,考虑扩张的思路。
思路如下:
1.首先找起始位能到达的范围是否覆盖了最终位置,并记录下搜索中的最远能到达的位置值,即max{nums[i] + i};
2.如果无法到达最终位置,则跳数加一,并从上一次搜索的最后位置开始,向后搜索到上一次记录的最大值所在的位置。
3.重复上面两步,直到找到最终跳数。
注意:
当数组元素只有1个时,跳数是0;
跳数的初值应该是1。
int jump(vector<int>& nums){
if (nums.size() < 2)return 0;//数组长度小于2
int start = 0, next = nums.at(0),end = nums.size() - 1;
int jump = 1,cur = next;//jump记录跳数,cur记录当前的最大范围
while (cur < end){
jump++;
for (int i = start + 1; i <= next; i++)
{
if (nums.at(i) + i >= end)return jump;
else if (nums.at(i) + i > cur)cur = nums.at(i) + i;
}
start = next;//start是搜索的开始位置
next = cur;//next是搜索的结束位置
}
return jump;
}
[LeetCode]Jump GameII的更多相关文章
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode——Jump Game II
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [leetcode]Jump Game @ Python
原题地址:https://oj.leetcode.com/problems/jump-game/ 题意: Given an array of non-negative integers, you ar ...
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- LeetCode: Jump Game Total 解题报告
Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of ...
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- restapi(6)- do it the functional way, 重温函数式编程
再次看了看上篇博客的源代码,发现连自己都看不懂了.想是为了赶时间交货不知不觉又回到OOP行令模式了,看看下面这段代码: (post & parameters('pid,'desc.?,'wid ...
- css3弹性盒子 flex布局
CSS3 弹性盒 1.display:flex 说明: 设置为弹性盒(父元素添加) 2.flex-direction(主轴排列方式) 说明: 顺序指定了弹性子元素在父容器中的位置 row 默认在一行内 ...
- 分享:个人APP(非企业资质)的微信登录方案
目前微信开放平台个人主体类APP不支持开通微信登录,那么个人开发者如何解决微信登录的问题呢?目前有一种替代方案是用微信小程序作为媒介来达到微信登录的目的. 微信小程序的登录无需企业资质,同时登录后返回 ...
- 13张PPT带你了解主动式消息队列处理集群
前言 偷偷和你们说,我搞了一份内部资料,该内部资料共有13张PPT,据作者透露,该PPT至少花了整整1周时间才编写完成,其内容简洁明了,内容深度足够,易于初学者理解,也给深度开发人员分享了不一样的消息 ...
- HDU 6134
题意略. 思路: 我们先不考虑[(i , j) == 1],在此情况下,其实这个值是sum( [ (i , j) == 1,2,3,....,n ] ) 这些情况.我们要求的仅仅是其中的第一部分而已. ...
- Leetcode之回溯法专题-51. N皇后(N-Queens)
Leetcode之回溯法专题-51. N皇后(N-Queens) n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给 ...
- Unity Shader 卡通渲染 基于退化四边形的实时描边
从csdn转移过来,顺便把写过的文章改写一下转过来. 一.边缘检测算法 3D模型描边有两种方式,一种是基于图像,即在所有3D模型渲染完成一张图片后,对这张图片进行边缘检测,最后得出描边效果.一种是基于 ...
- JSONP跨域的script标签请求为什么不受同源策略的限制?
在复习跨域的时候,复习到了JSONP跨域,大家都知道JSONP跨域是通过动态创建script标签,然后通过其src属性进行跨域请求的,前端需要一个数据处理的回调函数,而服务端需要配合执行回调函数,放入 ...
- 使用Docker快速部署ELK分析Nginx日志实践(二)
Kibana汉化使用中文界面实践 一.背景 笔者在上一篇文章使用Docker快速部署ELK分析Nginx日志实践当中有提到如何快速搭建ELK分析Nginx日志,但是这只是第一步,后面还有很多仪表盘需要 ...
- lightoj 1084 - Winter(dp+二分+线段树or其他数据结构)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1084 题解:不妨设dp[i] 表示考虑到第i个点时最少有几组那么 if a[i ...