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! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
随机推荐
- Jmeter-JDBC Request
1. 新建一个测试计划 2. 新建一个线程组 3. 创建数据库连接 4.配置数据库连接 5.添加JDBC Request 6.添加监听器
- TCP协议与流通信
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! TCP(Transportation Control Protocol)协议与IP ...
- 【LeetCode】042 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- 【LeetCode】040. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- bzoj 3501 PA2008 Cliquers Strike Back —— 贝尔数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3501 用贝尔三角预处理贝尔数,拆模数并在 \( p \) 进制下使用公式,因为这样每次角标增 ...
- sql 的基础语句
USE day15; -- 创建表CREATE TABLE teacher( id INT, NAME VARCHAR(20))-- 查看所有表SHOW TABLES; DESC student; D ...
- python list列表sort、sorted、reverse排序
列表排序:sort是修改原列表,sorted提供原列表的一个有序副本 li=[2,1,4,5,0]li.sort() #默认从小到大print li结果:[0, 1, 2, 4, 5] li=[2,1 ...
- 关于WPF的弹出窗口
几个重要的概念需要清楚: Show和ShowDialog区别 1.调用Show方法后弹出子窗口后,线程会继续往下执行.调用ShowDialog方法弹出子窗口后,线程会阻塞,直到子窗口关闭才继续往下执行 ...
- 《Java多线程编程核心技术》读后感(十三)
类InheritableThreadLocal的使用 使用类InheritableThreadLocal可以在子线程中取得父线程继承下来的值 值继承 package Third; import jav ...
- 第3章 编写ROS程序-2
1.发布者程序 在本节中,我们将看到如何发送随机生成的速度指令到一个turtlesim海龟,使它漫无目的地巡游.这个程序的源文件称为pubvel,这个程序展示了从代码中发布消息涉及的所有要素. 其代码 ...