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

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

For example:

Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step
from index 0 to 1, then 3 steps to the last index.)

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std; class Solution {
public:
int jump(vector<int> A)
{
int maxReach = A[0];
int edge = 0; //edge表示当前能够达到最远的坐标边界值
int minstep = 0; for (int i = 1; i < A.size(); i++)
{
//若当前坐标超过了最远的坐标边界值,应该跳跃一次,同一时候更新maxReach
if (i > edge)
{
minstep += 1;
edge = maxReach;
if (edge >= A.size() - 1) //若数组最后一个元素的坐标在edge覆盖的范围,则返回跳跃次数
return minstep;
}
maxReach = max(maxReach, A[i] + i);
}
//假设不能达到数组最后一个元素,则返回0
return 0;
}
}; int main()
{
Solution sol;
vector<int> nums = { 5,9,3,2,1,0,2,3,3,1,0,0 };
int res =sol.jump(nums);
cout << res << endl;
system("pause");
return 0;
}

经典算法——Jump Game(II)的更多相关文章

  1. 机器学习经典算法详解及Python实现--基于SMO的SVM分类器

    原文:http://blog.csdn.net/suipingsp/article/details/41645779 支持向量机基本上是最好的有监督学习算法,因其英文名为support vector  ...

  2. LeetCode: Jump Game II 解题报告

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

  3. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

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

  4. [leetcode解题记录]Jump Game和Jump Game II

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

  5. LeetCode 045 Jump Game II

    题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...

  6. Java中的经典算法之冒泡排序(Bubble Sort)

    Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...

  7. Atitit 图像处理30大经典算法attilax总结

    Atitit 图像处理30大经典算法attilax总结 1. 识别模糊图片算法2 2. 相似度识别算法(ahash,phash,dhash)2 3. 分辨率太小图片2 4. 横条薯条广告2 5. 图像 ...

  8. Java中的经典算法之选择排序(SelectionSort)

    Java中的经典算法之选择排序(SelectionSort) 神话丿小王子的博客主页 a) 原理:每一趟从待排序的记录中选出最小的元素,顺序放在已排好序的序列最后,直到全部记录排序完毕.也就是:每一趟 ...

  9. 57. Jump Game && Jump Game II

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

随机推荐

  1. JMeter 分布式测试部署

    对于并发量很大的需求,如上万并发量,受到CPU和内存的限制,单机模拟场景是实现不了的,为了让JMeter提供更大的负载能力,须使用它的分布式机制,即多台机器同时产生负载的功能. 以下参数分析可用于配置 ...

  2. KVM(八)使用 libvirt 迁移 QEMU/KVM 虚机和 Nova 虚机

    1. QEMU/KVM 迁移的概念 迁移(migration)包括系统整体的迁移和某个工作负载的迁移.系统整理迁移,是将系统上所有软件包括操作系统完全复制到另一个物理机硬件机器上.虚拟化环境中的迁移, ...

  3. 开放API端口SIGN算法详细设计

    开放API端口SIGN算法详细设计 前言 在app开放接口api的设计中,避免不了的就是安全性问题,因为大多数接口涉及到用户的个人信息以及一些敏感的数据,所以对这些接口需要进行身份的认证,那么这就需要 ...

  4. OpenAcc社区版安装教程(Linux版)(更新版)

    官方安装过程如下图所示 1.安装前 下载OpenAcc社区版 1,目前为止的最新版,平台是Linux,选择Linux x86-64. 我的服务器系统是CentOs 下载地址链接:https://www ...

  5. linux--redis的安装和配置和开启多个端口

    在workerman开发过程中需要安装redis来存储用户ip.端口等信息 首先UBUNTU中安装redis: apt-update  //更新apt包源apt-get install redis-s ...

  6. CSS基本属性—文本属性和背景属性

    一.CSS常用文本属性 [css中的颜色表示方式]   1.直接使用颜色的单词表示:red.green.blue    2.使用颜色的十六进制表示:#ff0000,#00ff00:    六位数,两两 ...

  7. win上使用nvm管理node版本

    win上使用nvm管理node版本 若想让nvm管理机器上所有的node版本,首先需要卸载电脑上已有的node(很重要), 然后下载nvm在win上的安装包 windows-nvm的下载地址 下载 下 ...

  8. docker 与 yarn

    有时我们的项目是使用yarn去发布的,当需要使用docker发布这个项目时,安装yarn是必须的,但是平时使用的npm install -g yarn此时却不可用 从网站上找到解决的方法 地址:htt ...

  9. php调用html添加超链接的方法

    不知道为何老搜不到,使用require_once命令导入的HTML文件没法加载js脚本,于是只能另谋他路,终于实线了,代码为 <?php header("Content-type:te ...

  10. 谜题12:ABC

    这个谜题要问的是一个悦耳的问题,下面的程序将打印什么呢? public class ABC{ public static void main(String[] args){ String letter ...