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;
}

最新文章

  1. Python全栈开发【基础四】
  2. 洛谷P1605 迷宫——S.B.S.
  3. Model Validation in ASP.NET Web API
  4. nginx 参数详解
  5. [转]hadoop hdfs常用命令
  6. windows2003安全加固脚本
  7. UCenter uc_user_synlogin同步登陆返回值为空(NULL)的解决办法 及 ucenter原理
  8. JavaScript trim 实现(去除字符串首尾指定字符)
  9. win7桌面图标小盾牌怎么去掉(2种方法)
  10. SQLServer 在Visual Studio的连接方法
  11. javascript call()与apply()
  12. testng xml 示例
  13. Django 缓存系统
  14. Cocoa包管理器之CocoaPods详解
  15. linux下安装redis并开机自启动
  16. 版本管理工具小乌龟TortoiseGit的安装和使用(2)
  17. BZOJ.1535.[POI2005]SZA-Template(KMP DP)
  18. [转]JVM内存模型
  19. 理解Backtracking
  20. 【转】JVM(Java虚拟机)优化大全和案例实战

热门文章

  1. [设计模式-行为型]观察者模式(Observer)
  2. Photoshop CC 2015
  3. Android sdk manager更新 下载API源码
  4. ubuntu 16.04 qtcreator 闪退
  5. [BZOJ2553][BeiJing2011]禁忌 dp+AC自动机+矩阵快速幂
  6. MyEclipse的破解代码,适用各个版本
  7. 洛谷 P2689 东南西北【模拟/搜索】
  8. OA项目实战(一) 概述
  9. 【C++】const 常引用的用法
  10. luogu P1313 计算系数