[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 ...
随机推荐
- Sqlserver2012 评估期已过问题
sql server 2012提示评估期已过的解决方法: 第一步:进入SQL2012配置工具中的安装中心. 第二步:再进入左侧维护选项界面,然后选择选择版本升级. 第三步:进入输入产品密钥界面,输入相 ...
- SpringBoot中关于Shiro权限管理的整合使用
转载:https://blog.csdn.net/fuweilian1/article/details/80309192 在整合Shiro的时候,我们先要确定一下我们的步骤: 1.加入Shiro的依 ...
- Docker系列之镜像瘦身(五)
前言 本节我们来讲讲在我们在构建镜像过程中不出问题,同时使得最后所构建的镜像文件大小尽可能最小,温馨提示:文中大图均可点击放大查看详细信息. 缓存(cache) Docker的优势之一在于提供了缓存, ...
- 【在 Nervos CKB 上做开发】Nervos CKB脚本编程简介[2]:脚本基础
CKB脚本编程简介[2]:脚本基础 原文作者:Xuejie 原文链接:Introduction to CKB Script Programming 2: Script 本文译者:Shooter,Jas ...
- cogs 80. 石子归并 动态规划
80. 石子归并 ★★ 输入文件:shizi.in 输出文件:shizi.out 简单对比时间限制:1 s 内存限制:128 MB 设有N堆沙(shi)子排成一排,其编号为1,2,3, ...
- unity_UGUI养成之路02
1.技能的冷确效果 2.背包的分页效果 1创建背包的总面板,并添加ToggleGroup组件 2.物品面板的实现 3.背包分页的实现 注意:添加了Toggle组件的游戏对象不能再添加button组件. ...
- 使用css实现水平垂直居中
1.通过absolute和margin实现(适用于弹窗,具体位置随浏览器屏幕大小变化改变)这种方式需要居中元素的父级必须采用绝对定位或相对定位,被居中元素的尺寸需要固定. <div class= ...
- Leetcode之回溯法专题-77. 组合(Combinations)
Leetcode之回溯法专题-77. 组合(Combinations) 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输 ...
- JavaScript的“true/false && expression”逻辑表达式
true/false && expression 在学习react的过程中,遇到了如下一个方法: function Mailbox(props) { const unreadMessa ...
- 洛谷P1196[NOI2002]银河英雄传说-并查集扩展
银河英雄传说 题意:在并查集的基础上,还要求出同一集合的两个点的距离 这道题用并查集自己是知道的,但是竟然可以这么骚的操作. 下面转自大佬的查详细题解 初见这道题,首先想到的方法当然是直接模拟,模拟每 ...