经典算法——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 ...
随机推荐
- picker鼠标上下拖动选择内容
上次写这个的时候的博客:http://hiuman.iteye.com/blog/2324929 上次是网上搜的,这次是自己写的. 无论多少个input都可以点击-但是只有一种内容(弹出的内容),可以 ...
- Log4Net的WinForm使用
一.Log4Net的WinForm使用 1.首先使用nuget 添加log4Net 到WinForm项目中 log4j每个符号的具体含义:%d %5p %c{1}:%L - %m%n log4j.pr ...
- pandas求五日线并画图
import pandas as pd import numpy as np import matplotlib.pyplot as plt stock_data = pd.read_csv('000 ...
- Selenium2+python自动化55-unittest之装饰器(@classmethod)【转载】
前言 前面讲到unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间. 于是就想是不是可以 ...
- MATLAB二维插值和三维插值
插值问题描述:已知一个函数上的若干点,但函数具体表达式未知,现在要利用已知的若干点求在其他点处的函数值,这个过程就是插值的过程. 1.一维插值 一维插值就是给出y=f(x)上的点(x1,y1),(x2 ...
- django web 自定义通用权限控制
需求:web系统有包含以下5个url,分别对于不同资源: 1.stu/add_stu/ 2.stu/upload_homework/ 3.stu/query_homework/ 4.stu/add_r ...
- 执行程序(例如UltraEdit)在WIN7下添加到右键菜单
把下面提供的代码复制到记事本,保存为注册表文件(*.reg),右键合并即可.注意把最后一行换成你自己的路径. Windows Registry Editor Version 5.00 [HKEY_CL ...
- [ThinkPHP] 从一个表中获得栏目对应的ID,从另一个表获得属于这些栏目的文章
public function index(){ $cate = 1; $query = M('Cate')->field('id')->where(array('id'=>$cat ...
- POJ 1321 棋盘问题 (DFS + 回溯)
题目链接:http://poj.org/problem?id=1321 题意:中文题目,就不多说了...... 思路: 解题方法挺多,刚开始想的是先从N行中选择出来含有“#”的K行,再在这K行中放置K ...
- 学习LSM(Linux security module)之三:Apparmor的前世今生和基本使用
感冒了,感觉一脑子浆糊,真是蛋疼. 先粗略讲一些前置知识. 一:MAC和DAC DAC(Discretionary Access Control),自主访问控制,是最常用的一类访问控制机制,意思为主体 ...