题目: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的更多相关文章

  1. [LeetCode] Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. [LeetCode] Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. LeetCode——Jump Game II

    Description: Given an array of non-negative integers, you are initially positioned at the first inde ...

  4. [leetcode]Jump Game @ Python

    原题地址:https://oj.leetcode.com/problems/jump-game/ 题意: Given an array of non-negative integers, you ar ...

  5. LeetCode: Jump Game II 解题报告

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  6. LeetCode: Jump Game Total 解题报告

    Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of ...

  7. 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways

    引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...

  8. [LeetCode] Jump Game II 贪心

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. Leetcode jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. 在vps上安装 kali linux

    在渗透测试过程中,很多时候我们需要反弹一个shell回来.使用empire也好,MSF也好,其他工具也好,都避不开公网IP的问题.这时候我们就需要一个VPS来进一步进行渗透测试. 建立通道连接的方式有 ...

  2. html中video标签

    video标签: <video src="视频的后缀名是.webM/.ogg/.mp4 "></video>属性: src:用于告诉video标签需要播放的 ...

  3. CSS布局:元素水平居中

    CSS布局之元素水平居中 本文将依次介绍在不同条件下实现水平居中多种方法 一.使用 text-align: center : 适用于块级元素内部的行内元素水平居中(也适用于图片的水平居中) 此方法对i ...

  4. mysql/mariadb 初体验

    距离申请这个博客号已经过了九个月,思前想后还是把知识沉淀放这里吧,不过初心一样,依旧是 '谨以此文,见证成果'.有 兴趣的话也欢迎大家去我的csdn博客转一转.以下是正文: 1.mysql安装 win ...

  5. 2019年 iPad无法充电

    2019年 iPad无法充电  到售后网点检测没毛病,可能是apple产品做了低温保护逻辑机制低温无法充电,或者说是冬天温度跟iPad电池充电温度要求不符.各位有遇到情况的可以看看是不是这种问题,这问 ...

  6. 操作系统-IO管理概述

    IO管理概述 一.IO设备 IO设备管理是操作系统设计中最凌乱也最具挑战性的部分.由于它包含了很多领域的不同设备以及与设备相关的应用程序,因此很难有一个通用且一直的设计方案.所以在理解设备管理之前,应 ...

  7. 牛客-长沙理工校赛C-取手机

    传送门:https://www.nowcoder.com/acm/contest/96/C 参考:http://www.cnblogs.com/Dillonh/p/8835074.html 题意: d ...

  8. lightoj 1126 - Building Twin Towers(dp,递推)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1126 题解:一道基础的dp就是简单的递推可以设dp[height_left][ ...

  9. 玲珑杯”ACM比赛 Round #18 A -- 计算几何你瞎暴力(瞎暴力)

    题目链接:http://www.ifrog.cc/acm/problem/1143?contest=1020&no=0 题解:就是瞎暴力具体多暴力看一下代码就知道了. #include < ...

  10. codeforces 812 E. Sagheer and Apple Tree(树+尼姆博弈)

    题目链接:http://codeforces.com/contest/812/problem/E 题意:有一颗苹果树,这个苹果树所有叶子节点的深度要不全是奇数,要不全是偶数,并且包括根在内的所有节点上 ...