题目要求: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.)

代码如下:

class Solution {
public:
int jump(int A[], int n) { int step = 0;
int left = 0;
int right = 0; //用来记录局部点的最大步长 if(n == 1) return 0; while(left <= right){
step++;
const int old_right = right; //贪心算法
//局部最优解
for(int i = left; i <= old_right; i++){
//计算每次到达的地方(局部)
int new_right = i + A[i];
//如果new_right超过n-1,则表示到了结尾
if(new_right >= n - 1) return step;
//保证right是最大
if(new_right > right) right = new_right;
} //左值放在最优解的下一个
left = old_right + 1;
} return 0; }
};

最新文章

  1. C# 文章导航
  2. php分页原理
  3. Ubuntu 14.04 更换阿里云源
  4. 攻克Spring
  5. find exec 运用
  6. hadoop配置文件加载顺序(转)
  7. HTML5 File api 实现断点续传
  8. IIS部署FTP服务器步骤
  9. 全民Scheme(0):lat的定义
  10. 【EntityFramework 6.1.3】个人理解与问题记录(2)
  11. python3 进一步了解装饰器 NLP第四条
  12. centos7防火墙配置
  13. 配置php环境的一个nginx.conf
  14. python - 字符编码/格式化/替换符
  15. 一个简易的netty udp服务端
  16. django-CRM-项目部署
  17. js改变或添加className
  18. jqgrid 行选中multiboxonly属性说明
  19. HttpComponents-Client学习
  20. JS修改属性,六种数据类型

热门文章

  1. mysql管理表关系
  2. 微信小程序跳转到微信公众号
  3. CF1271E Common Number
  4. iNeuOS工业互联平台,WEB组态(iNeuView)增加工程视图导入、导出功能,及优化和修复,发布:v3.2.1版本
  5. Ubuntu17.10 React Native 环境搭建
  6. leetcode110:combination-sum-ii
  7. 易于理解的 python 深度学习摘要算法教程
  8. ElementUI表格行编辑单元格编辑支持(输入框,选择框)Demo
  9. Netty源码解析 -- 零拷贝机制与ByteBuf
  10. 这才是图文并茂:我写了1万多字,就是为了让你了解AQS是怎么运行的