【一天一道LeetCode】#45. Jump Game II
一天一道LeetCode系列
(一)题目
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.)
Note:
You can assume that you can always reach the last index.
(二)解题
1、首先想到的做法就是对于序号i,判断i+nums[i]能不能到达终点,如果能直接return,如果不能对小于nums[i]的数依次进行递归,得到最小的min值。这种做法直接超时了,TLE!
class Solution {
public:
int min;
int jump(vector<int>& nums) {
min = nums.size();
jumpdfs(nums,0,0);
return min;
}
void jumpdfs(vector<int>& nums , int idx , int count)
{
int len = nums.size();
if(idx >= len -1) {
min = count;
return;
}
if(idx<len && idx + nums[idx] >= len-1){
min = min>(count+1)?(count+1):min;
return;
}
else
{
for(int i = 1 ; idx<len && i<= nums[idx] ; i++)
{
jumpdfs(nums,idx+i,count+1);
}
}
}
};
2、然后看到提示中有提到贪心算法,于是联想到昨天做的通配符那题,可以记录上一跳能达到的最远位置,和当前跳所能到达的最远位置,每次循环就更新这两个值,直到上一跳能达到终点就退出。
/*
思路:贪心算法是假定每一次都作出当前的最优解,所以对于本题,每次记录当前的最远位置,和上一跳的最远位置。
*/
class Solution {
public:
int jump(vector<int>& nums) {
int ret = 0 ;
int last = 0;
int cur = 0;
for(int i = 0 ; i < nums.size() ; i++)
{
if(last>=nums.size()-1) break;
if(i>last)//如果当前位置已经跳出了上一跳的最远位置
{
last = cur;//更新上一跳的最远位置等于当前跳的最远位置
++ret;//跳的次数加1
}
cur = max(cur , i+nums[i]);//更新当前跳的最远位置
}
return ret;
}
};
【一天一道LeetCode】#45. Jump Game II的更多相关文章
- 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 解题思路
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 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...
- 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(hard)
原题 题意: 是Jump Game的衍生题(题解),题意求跳到最后一格所需最少步数,(默认所测数据永远可以跳到最后一格). 思路: 利用贪心,遍历数组,记录在每一步可跳跃到的最大区域. 1.当前步数 ...
- [LeetCode] 45. Jump game II ☆☆☆☆☆(跳跃游戏 2)
https://leetcode-cn.com/problems/jump-game-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-b ...
随机推荐
- 使用WeihanLi.Redis操作Redis
WeihanLi.Redis Intro StackExchange.Redis 扩展,更简单的泛型操作,并提供一些的适用于业务场景中的扩展 基于 Redis 的五种数据类型扩展出了一些应用: Str ...
- Java内存泄漏分析系列之一:使用jstack定位线程堆栈信息
原文地址:http://www.javatang.com 前一段时间上线的系统升级之后,出现了严重的高CPU的问题,于是开始了一系列的优化处理之中,现在将这个过程做成一个系列的文章. 基本概念 在对J ...
- Android Design Support Library使用详解——Snackbar
Google在2015 I/O大会上,给我们带来了更加详细的Material Design规范,同时也引入了Android Design Support Library,为我们提供了基于Materia ...
- Bootstrap3 排版-列表
无序列表 排列顺序无关紧要的一列元素. <ul> <li>...</li> </ul> 有序列表 顺序至关重要的一组元素. <ol> < ...
- 优先使用TimeUnit类中的sleep()
TimeUnit是什么? TimeUnit是java.util.concurrent包下面的一个类,TimeUnit提供了可读性更好的线程暂停操作,通常用来替换Thread.sleep(),在很长一段 ...
- CSS3 滤镜学习
html篇 样式篇 grayscale sepia saturate hue-rotate invert opactiy brightness contrast blur drop-shadow 综合 ...
- springMVC源码分析--DispatcherServlet请求获取及处理
在之前的博客springMVC源码分析--容器初始化(二)DispatcherServlet中我们介绍过DispatcherServlet,是在容器初始化过程中出现的,我们之前也说过Dispatche ...
- 树莓派初体验,安装Ubuntu 14.04 LTS
转载自:http://www.polarxiong.com/archives/%E6%A0%91%E8%8E%93%E6%B4%BE%E5%88%9D%E4%BD%93%E9%AA%8C-%E5%AE ...
- Erlang简单并行服务器
Erlang简单并行服务器(金庆的专栏)Erlang并行服务器为每个Tcp连接创建对应的连接进程,处理客户端数据.参考 Erlang程序设计(第2版)17.1.3 顺序和并行服务器并行服务器的诀窍是: ...
- scala模式匹配的使用
Scala模式匹配 Tip1:模式总是从上往下匹配,如果匹配不到则匹配case_项(类似Java中的default) Tip2:与Java和C语言不同,不需要在每个分支末尾使用break语句退出(不会 ...