经典算法——Jump Game(II)
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)的更多相关文章
- 机器学习经典算法详解及Python实现--基于SMO的SVM分类器
原文:http://blog.csdn.net/suipingsp/article/details/41645779 支持向量机基本上是最好的有监督学习算法,因其英文名为support vector ...
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [leetcode解题记录]Jump Game和Jump Game II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- LeetCode 045 Jump Game II
题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...
- Java中的经典算法之冒泡排序(Bubble Sort)
Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...
- Atitit 图像处理30大经典算法attilax总结
Atitit 图像处理30大经典算法attilax总结 1. 识别模糊图片算法2 2. 相似度识别算法(ahash,phash,dhash)2 3. 分辨率太小图片2 4. 横条薯条广告2 5. 图像 ...
- Java中的经典算法之选择排序(SelectionSort)
Java中的经典算法之选择排序(SelectionSort) 神话丿小王子的博客主页 a) 原理:每一趟从待排序的记录中选出最小的元素,顺序放在已排好序的序列最后,直到全部记录排序完毕.也就是:每一趟 ...
- 57. Jump Game && Jump Game II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
随机推荐
- 【 HAProxy 】学习笔记
一.haproxy的功能: HAProxy vs LVS HAProxy支持tcp和http两种代理模式,而lvs仅支持tcp代理模式 HAProxy相比LVS的使用要简单 ...
- Android 用AsyncTask下载网络图片并显示百分比
1.添加布局文件:activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a ...
- thinkphp5最美跳转页面
声明下:此教程来自TP官网,如果需要看原文,请点击一下链接 http://www.thinkphp.cn/code/3437.html 先给大家看下效果: 直接撸代码: 第一步:为了增加对移动设备 ...
- thinkphp5.0配置nginx重写规则
我的centeros7.3系统,单主机实例: vi /usr/local/nginx/conf/nginx.conf 在如图红框位置加入以下代码(图片是截取的,截取部分上方还有其他的默认配置代码,代码 ...
- Yii2中如何访问controller的二级目录下的控制器
Yii2默认的控制器下只有一层目录放在controller目录下,当开发的需要的时候需要在该目录下面新增一层目录也就是在controller下增加二级目录,比如controller/api/Membe ...
- 将datatable导出为excel的三种方式(转)
一.使用Microsoft.Office.Interop.Excel.DLL 需要安装Office 代码如下: 2 public static bool ExportExcel(Sy ...
- Submile text3 安装使用技巧
第一次用submile text3,发现还是有一些东西需要配置的,而且submile text3是最近发布的,和原来的submile text2还是有一些区别的.这两天自己配置了一下,用起来感觉还是不 ...
- Python:文件操作技巧(File operation)(转)
Python:文件操作技巧(File operation) 读写文件 # ! /usr/bin/python # -*- coding: utf8 -*- spath = " D:/dow ...
- POJ 1163.The Triangle-动态规划
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 50122 Accepted: 30285 De ...
- AMQ学习笔记 - 02. JMS客户端编程模型
概述 客户端编程模型,是讲如何使用JMS API实现Java应用程序和JMS Provider的通信. 消息传送模式 消息传送模式又称为消息传送域,JMS API定义了两种模式:PTP和Pub/Sub ...