045 Jump Game II 跳跃游戏 II
给定一个非负整数数组,你最初位于数组的首位。
数组中的每个元素表示你在该位置的最大跳跃长度。
你的目标是用最小跳跃次数到达最后一个索引。
例如:
给定一个数组 A = [2,3,1,1,4]
跳到最后一个索引的最小跳跃数是 2。(从索引 0 跳到 1 跳1步,然后跳3步到最后一个索引。)
注意:
假设你总是可以到达最后一个索引位置。
详见:https://leetcode.com/problems/jump-game-ii/description/
Java实现:
curReach是维护的当前能跳到的最大位置,maxReach是指从之前的点能到达到得最远位置。
当i 大于之前点能跳到的最大位置时,就需要跳一步,并把curReach更新为maxReach。
最后返回若是curReach能到最后一个元素,就返回step, 若是到不了,就说明根本走不到最后一步,返回0。
参考:http://www.cnblogs.com/Dylan-Java-NYC/p/4834079.html
class Solution {
public int jump(int[] nums) {
int n=nums.length;
int step=0;
int curReach=0;
int maxReach=0;
for(int i=0;i<n;++i){
if(curReach<i){
++step;
curReach=maxReach;
}
maxReach=maxReach>(nums[i]+i)?maxReach:(nums[i]+i);
}
return maxReach<n-1?0:step;
}
}
C++实现:
class Solution {
public:
int jump(vector<int>& nums) {
int n=nums.size();
int res=0;
int curRch=0;
int maxRch=0;
for(int i=0;i<n;++i)
{
if(curRch<i)
{
++res;
curRch=maxRch;
}
maxRch=max(maxRch,nums[i]+i);
}
return res;
}
};
参考:http://www.cnblogs.com/ganganloveu/p/3761715.html
045 Jump Game II 跳跃游戏 II的更多相关文章
- 【LeetCode每天一题】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 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- lintcode: 跳跃游戏 II
跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A = ...
- Leetcode力扣45题 跳跃游戏 II
原题目: 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: ...
- LeetCode 45. 跳跃游戏 II | Python
45. 跳跃游戏 II 题目来源:https://leetcode-cn.com/problems/jump-game-ii 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...
- Java实现 LeetCode 45 跳跃游戏 II(二)
45. 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- [leetcode] 45. 跳跃游戏 II(Java)(动态规划)
45. 跳跃游戏 II 动态规划 此题可以倒着想. 看示例: [2,3,1,1,4] 我们从后往前推,对于第4个数1,跳一次 对于第3个数1,显然只能跳到第4个数上,那么从第3个数开始跳到最后需要两次 ...
- leetcode 55. 跳跃游戏 及 45. 跳跃游戏 II
55. 跳跃游戏 问题描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...
- LeetCode(45): 跳跃游戏 II
Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
随机推荐
- python基础-正则2
正则函数 Python提供re模块,包含所有正则表达式的功能 由于python的字符串本身也有\转义,所以需要注意: s = "ABC\\-001" 对应的正则表达式应为:'ABC ...
- jmeter-sampler(取样器)HTTP请求
名称:用于标识一个sample. 注释:对于测试没任何影响,仅用来记录用户可读的注释信息. 服务名称或IP:http请求发送的目标服务器名称或者IP地址,比如:http://www.baidu.com ...
- listen 56
Kettles Stop Whistling in the Dark British physicist Lord Rayleigh is best known for his discovery o ...
- java面试题05
1.写一个冒泡排序的算法 升序排列: public static void main(String[] args) { int score[] = { 67, 20, 75, 87, 89, 90, ...
- ACM学习历程—HDU5396 Expression(递推 && 计数)
Problem Description Teacher Mai has n numbers a1,a2,⋯,an and n−1 operators("+", "-&qu ...
- Restore Points 制定回退方案
Restore Points 制定回退方案 背景:Flashback Database 和 restore points 都可以提供一个基于时间点的回滚. 理论:1) Normal Restore P ...
- BZOJ3524:[POI2014]Couriers
浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...
- poj2279排队——杨氏矩阵与钩子公式(DP爆内存)
题目:http://poj.org/problem?id=2279 书上的DP做法会爆内存,尝试写了一个,过了样例. 转载: 代码如下: #include<iostream> #inclu ...
- RT-Thread OS的启动流程
1.RT进入main之前, SystemInit函数初始化时钟. 2.main函数位于startup.c文件中.进行两个工作 系统开始前,rt_hw_interrupt_disable关闭所有中断. ...
- java递归demo---
递归思想: 递归就是方法里调用自身 在使用递归策略时,必须有一个明确的递归结束条件,称为递归出口 递归算法代码显得很简洁,但递归算法解题的运行效率较低.所以不提倡用递归设计程序. 在递归调用的过程中系 ...