LeetCode56:Jump Game
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.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
Array Greedy
解法一
最開始考虑的是用递归求解,对于A=[2,3,1,1,4]这个数组,能够
从头開始遍历,假设对于上面的第一个元素2,能够前进两步,然后分别求两个数组A1=[3,1,1,4]和A2=[1,1,4]的结果,例如以下图:这样的方法能够求解出结果,可是从图中不难看出会有大量的反复,结果在leetCode中也显示超时
还是将代码列出:
class Solution {
public:
bool canJump(vector<int>& nums) {
int result=0;
canJumpChild(nums,0,result);
return result;
}
void canJumpChild(vector<int> & nums,int offset,int &result)
{
if(result==1)
return ;
if(offset==(nums.size()-1))
{
result=1;
return ;
}
vector<int>::iterator iter=nums.begin()+offset;
for(int i=1;i<=*iter;i++)
canJumpChild(nums,offset+i,result);
}
};
然后看到了leetcode的提示greedy,就是说能够用贪婪算法来求解这个问题。发现这是一道很easy的用贪婪发就能够求解的,以下解法二和解法三都是这个思路。
解法二
贪婪策略依据还能向前移动的步长来推断,从第二个元素開始循环,假设能移动的步长小于等于0。表示无法到达这一步,就返回false。否则依据当前索引处的值来更新能移动的步长。代码例如以下:
class Solution {
public:
bool canJump(vector<int>& nums) {
//remain_step记录剩下的步数。表示最多能向前移动几步
int remain_step = nums.front();
//i能够理解成是否能到达的下标处。注意是从下标为1的位置開始,假设循环到数组的末端还能向前移动表示能到达末端
for(int i = 1; i<nums.size(); i++) {
//当这个值降低到0。无法进一步向前移动
if(remain_step <= 0) return false;
//更新这个值
remain_step = max(--remain_step, nums[i]);
}
return true;
}
};
解法三
贪婪策略是能到达的最远处,每次到达一个下标处后就更新能到达的最远处的值。
class Solution {
public:
bool canJump(vector<int>& nums) {
int max_jump=0;//max_jump表示能到达的最远的地方的下标。初始在0处
//跳出循环的条件是已经走到了最远处
for(int i=0;i<=max_jump;i++)//max_jump表示能到达的最远位置的下标
{
//假设能到达的最远位置的下标大于等于nums.size()-1。表示能到达末尾
if(max_jump>=nums.size()-1)
return true;
if(i+nums[i]>max_jump)
max_jump=i+nums[i];//更新能到达的最远的地方
}
return false;
}
};LeetCode56:Jump Game的更多相关文章
- [LeetCode] Frog Jump 青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 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 55. Jump Game
我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...
- LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- bug report: Jump to the invalid address stated on the next line at 0x0: ???
gdb或者vlagrind报告: ==14569== Jump to the invalid address stated on the next line ==14569== at 0x0: ??? ...
随机推荐
- Set Matrix Zeroes——常数空间内完成
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Did yo ...
- 四十四 常用内建模块 struct
准确地讲,Python没有专门处理字节的数据类型.但由于str既是字符串,又可以表示字节,所以,字节数组=str.而在C语言中,我们可以很方便地用struct.union来处理字节,以及字节和int, ...
- Codeforces 1131 B. Draw!-暴力 (Codeforces Round #541 (Div. 2))
B. Draw! time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- 洛谷——P1113 杂务
P1113 杂务 题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务 ...
- Xamarin Android SDK无法更新的解决办法
Xamarin Android SDK无法更新的解决办法 Xamarin Android SDK无法更新的解决办法,更新时候,提示警告信息:A folder failed to be moved. ...
- 【BZOJ 1078】 1078: [SCOI2008]斜堆
1078: [SCOI2008]斜堆 Description 斜堆(skew heap)是一种常用的数据结构.它也是二叉树,且满足与二叉堆相同的堆性质:每个非根结点的值都比它父亲大.因此在整棵斜堆中, ...
- android View post(Runnable runnable ) 新线程
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha View post(Runnable runnable ) 方法 不会 创建 新线程, ...
- 随机数选择器 Exercise07_13
import java.util.Scanner; /** * @author 冰樱梦 *时间:2018年下半年 *题目:随机数选择器 */ public class Exercise07_13 { ...
- mysql 下 计算 两点 经纬度 之间的距离 含具体sql语句
文章转载地址 http://blog.sina.com.cn/s/blog_7bbfd5fd01017d1e.html 感谢作者. 在原文的基础上,我新增了sql语句,方便大家理解 mysql距离计算 ...
- 陈立伟 - MultiCharts快易通(2013年8月2日)
<MultiCharts快易通> 作 者:陈立伟 译 者: 系 列:寰宇程式交易312--挑战程式交易系列1 出 版:寰宇出版股份有限公司 字 数:千字 阅读完成:2013年8月2日