题目要求: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; }
};

LeetCode 045 Jump Game II的更多相关文章

  1. Java for LeetCode 045 Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  3. [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  5. [LeetCode] 45. Jump Game II 跳跃游戏 II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. [LeetCode] 45. Jump Game II 解题思路

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. LeetCode 45 Jump Game II(按照数组进行移动)

    题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description   给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...

  8. 045 Jump Game II 跳跃游戏 II

    给定一个非负整数数组,你最初位于数组的首位.数组中的每个元素表示你在该位置的最大跳跃长度.你的目标是用最小跳跃次数到达最后一个索引.例如: 给定一个数组 A = [2,3,1,1,4]跳到最后一个索引 ...

  9. [LeetCode] 45. Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. python实现城市气候与海洋的关系研究

    城市气候与海洋的关系研究 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 后打开浏览器输入网址http:// ...

  2. 如何对List集合中的对象进行按某个属性排序

    我们在实际的开发工作中,经常会碰到排序的问题,如题,我们如何针对List集合中的某一个属性进行排序 当list集合中的元素类型是我们自定义类型时,有两种对list中的元素进行排序的方法: 方法一 让l ...

  3. js音乐播放器【简洁】

    辞职的第二天没有去找工作还,准备回家. 但到了火车站才发现沃特玛的买的票不是在这个火车站坐. 这就耽误了行程...... 说出来真舒服!!!淦 代码 这里已经上传到码云了,大家可以直接引用. 目前只有 ...

  4. (五)F5和CTRL+F5两种刷新的区别

    一.刷新原理不同: F5触发的HTTP请求的请求头中通常包含了If-Modified-Since 或 If-None-Match字段,或者两者兼有. CTRL+F5触发的HTTP请求的请求头中没有上面 ...

  5. 多服务器使用Docker设置一主一从三哨兵redis(完整)

    本来应该续之前那篇博客Docker配置redis哨兵模式--多服务器·上写一个下篇的,但是忽然意识到应该将必要的环境打包为一个基础镜像,在此基础上建立与redis有关的镜像,这样既能够快速打包,又能够 ...

  6. sdsdsd

    create PROCEDURE b2(in c_year int,in co int)begin declare num int; if exists(select * from class whe ...

  7. Python_环境搭建_jupyterNotebook的使用

    # @ Author : Collin_PXY # 虚拟环境的创建及Jupyter Notebook的基本使用 # Anaconda 和 Jupter Notebook的使用: # 首先得需要安装 A ...

  8. Docker - 解决在容器内删除和主机映射的目录而报错 rm: cannot remove 'webapps': Device or resource busy 的问题

    问题背景 docker run -d --name tomcat7 -v /usr/local/tomcat/webapps:/usr/local/tomcat/webapps tomcat:7 使用 ...

  9. java1.8安装及环境变量配置

    一.前言 虽然jdk1.9版本已经问世,但是许多其他的配套设施并不一定支持jdk1.9版本,所以这里仅带领你配置jdk1.8.而jdk1.9的操作也几乎是相同的. 本教程适用于windows10 64 ...

  10. 利用s3-test进行ceph的接口兼容性测试

    前言 ceph的rgw能够提供一个兼容性的s3的接口,既然是兼容性,当然不可能是所有接口都会兼容,那么我们需要有一个工具来进行接口的验证以及测试,这个在其他测试工具里面有类似的posix接口验证工具, ...