题目要求: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. Java线程知识拾遗

    知识回顾 进程与线程是常常被提到的两个概念.进程拥有独立的代码段.数据空间,线程共享代码段和数据空间,但有独立的栈空间.线程是操作系统调度的最小单位,通常一个进程会包含一个或多个线程.多线程和多进程都 ...

  2. 浅谈 Johnson 算法

    目录 前言 引入 算法概述 算法流程 正确性证明 代码实现 结语 前言 Johnson 和 Floyd 一样是用来解决无负环图上的全源最短路. 在稀疏图上的表现远远超过 Floyd,时间复杂度 \(O ...

  3. 电脑查看当前自己的wifi密码

    菜单+R 输入control  点击确认.

  4. VC6最基本

    高级语言C++程序设计[chap4][p119-1][我的自考书-刘璟周玉龙书] 1.VC6使用: <1>.编译:"Build"-->"Compile& ...

  5. Quirc二维码识别模块

    背景 师兄要用这个参加集赛,但是说自己C语言不太行,花了一个多小时帮他分析了一下(虽然自己也不太行). 对应模块的GitHub地址:https://github.com/dlbeer/quirc. 记 ...

  6. mysql中delete from t1 where id = 10加锁状况叙述

    在Next_Key Lock算法中,不仅仅锁定住所找到的索引,而且还锁定住这些索引覆盖的范围.因此在这个范围内的插入都是不允许的.这样就避免了在这个范围内插入数据导致的幻读问题. delete fro ...

  7. centos6安装calamari

    安装操作系统 首先安装操作系统centos6,安装过程选择的是base server,这个不相同不要紧,出现缺少包的时候去iso找出来安装就可以了 calamari的简单介绍 首先简单的介绍下cala ...

  8. 使用ViewPager实现卡片叠加效果

    使用ViewPager实现卡片叠加效果 背景 在开发项目时,需要对 App的某个资源模块进行界面重构,其中在资源展示部分中新的交互以卡片叠加的效果替代了原来的资源组织树门禁展示方式.在新的资源展示方式 ...

  9. 一个List按照某个size分割为多个小的List

    针对于List的size比较大,使用多线程处理任务时,可以将List分割为一个一个比较小的任务单元进行处理. 例如集合大小:645,按照100分割,会将集合分割为6个size为100的集合和一个siz ...

  10. [原题复现]ByteCTF 2019 –WEB- Boring-Code[无参数rce、绕过filter_var(),等]

    简介  原题复现:  考察知识点:无参数命令执行.绕过filter_var(), preg_match()  线上平台:https://buuoj.cn(北京联合大学公开的CTF平台) 榆林学院内可使 ...