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. centos 7 卸载自带的jdk

    # 查看jdk安装信息 rpm -qa|grep java 卸载已安装的jdk: # yum -y remove java java-1.7.0-*

  2. MATLAB的简单动画制作

    这里介绍两种类型的动画实现,一种使用getframe和movie命令实现帧动画,另一种使用comet(comet3)命令实现画图过程的动画. ①getframe和movie命令实现帧动画 例如,创建一 ...

  3. redis特性与使用场景

    一.8大特性 1.速度快 数据存储在内存,可达到10万OPS 2.可持久化,断电不丢数据 所有数据保存在内存中,对数据的更新异步的保存在硬盘中 3.多种数据结构 字符串.哈希.列表.集合.有序集 合位 ...

  4. bash特性

    (1)shell简介 shell是系统的用户界面,提供用户与内核进行交互操作的一种接口,它接收用户输入的命令并把它送入内核去执行.实际上shell是一个命令解释器 (2)shell语法 语法:命令 选 ...

  5. sonarQube Github pull request扫描代码

    参考官方地址:https://docs.sonarqube.org/display/PLUG/GitHub+Plugin 运行环境:sonarQube6.2 + sonarScanner2.8 近来, ...

  6. System Center VMM请注意不同语言版本的差异

    在私有云的项目中,经常需要判断System Center一些组件的连接是否OK. 我这里有开发,和测试两个环境,开发是英文版的System Center VMM,测试用的是中文版的System Cen ...

  7. HDU 2673 (排序)

    Acmer in HDU-ACM team are ambitious, especially shǎ崽, he can spend time in Internet bar doing proble ...

  8. MySQL密码不能登陆问题

        由于种种原因,在进行开发的时候我一直是基于Windows平台,并且以前初学的时候常常重装不同版本的 MySQL数据库.因此长时间不使用后就产生了一些冲突的问题.     简单描述下,今天用以前 ...

  9. 正则 lazy

  10. ASP.net 资源请求漏洞利用工具PadBuster

    ASP.net 资源请求漏洞利用工具PadBuster 在ASP.net 网站中,为了便于部署网站项目,开发者往往会将资源(图片.Javascript文件)嵌入到dll文件中.而网页中,会使用WebR ...