LeetCode 045 Jump Game II
题目要求: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的更多相关文章
- 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 ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetCode 45.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 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 45 Jump Game II(按照数组进行移动)
题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...
- 045 Jump Game II 跳跃游戏 II
给定一个非负整数数组,你最初位于数组的首位.数组中的每个元素表示你在该位置的最大跳跃长度.你的目标是用最小跳跃次数到达最后一个索引.例如: 给定一个数组 A = [2,3,1,1,4]跳到最后一个索引 ...
- [LeetCode] 45. Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- Java学习的第一天
1.今天安装了Java操作环境,并运行了helloworld的程序 2.在安装过程中,文件路径丢了,与c++和c之前的差异使我难堪 3.明天准备完成小学期任务以及继续往下学习Java语言读大道至简
- P2346 四子连棋
P2346 四子连棋 迭代加深++ 题意描述 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋 ...
- 【Luogu】P2292 [HNOI2004]L语言 题解
前置芝士:\(Trie\)字典树 这道题,说是AC自动机,实际上一个\(Trie+\)队列轻松搞定. 首先,我们对所有单词建一棵\(Trie\). 然后,定义一个空队列\(Q\),初始时把\(-1\) ...
- 【无思路题目】Leetcode-1640. 能否连接形成数组
[JAVA]参考题解 1.思路是这样子的,先用哈希表的key存下pieces的每一行的第一个元素即p[0],然后value存放相应的一维数组: 2.然后遍历arr数组,先查看每个值是否在map中,若存 ...
- 排序算法—快速排序(Quick Sort)
快速排序(Quick Sort) 快速排序的基本思想:通过一趟排序将待排记录分隔成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序. ...
- Spring Security 实战干货:客户端OAuth2授权请求的入口
1. 前言 在Spring Security 实战干货:OAuth2第三方授权初体验一文中我先对OAuth2.0涉及的一些常用概念进行介绍,然后直接通过一个DEMO来让大家切身感受了OAuth2.0第 ...
- leetcode144 longest-palindromic-substring
题目描述 找出给出的字符串S中最长的回文子串.假设S的最大长度为1000,并且只存在唯一解. Given a string S, find the longest palindromic substr ...
- 09-jQuery案例:爱好选择器
爱好选择器HTML 1 <!DOCTYPE html> 2 <head> 3 <meta charset="UTF-8"> 4 <titl ...
- git clone克隆github仓库慢,问题解决
导读 转载自:https://www.hangge.com/blog/cache/detail_2670.html 原因 由于国内网络问题,当我们使用 git clone 命令从 github ...
- VMware虚拟机 - 如何让鼠标从虚拟机中返回到个人计算机中
最简单的方式 按快捷键:ctrl+alt即可 彻底解决问题的方法:安装VMware Tools 前提条件 开启虚拟机 确认客户机操作系统正在运行 因为 VMware Tools 安装程序是使用 Per ...