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: ??? ...
随机推荐
- 【转载】 ftp 命令行
原文在这里. 本文中,介绍在 Linux shell 中如何使用 ftp 命令.包括如何连接 FTP 服务器,上传或下载文件以及创建文件夹.尽管现在有许多不错的 FTP 桌面应用,但是在服务器.SSH ...
- Centos6.9下安装OpenOffice 4.1.4
# 对一下时间,时间不准,解压不了yum install -y ntp unzipntpdate -u 202.112.10.36yum install libXext.x86_64 -yyum gr ...
- 用ab进行POST,GET压力测试,且定义header及json的内容
这个以前没作过,找了文档.搞定. 要求时间的单curl测试的方式如下: time curl http://xxxxx/hq/restapi/isStart curl用来用post请求,且有header ...
- AC日记——餐巾计划问题 洛谷 P1084
餐巾计划问题 思路: 氧气优化水过: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 4005 #define ...
- CentOS7安装私有gitlab
1.安装依赖包 yum install -y curl policycoreutils openssh-server openssh-clients postfix systemctl start p ...
- For input string: "..."
之前在项目中通过EL表达式从cart 中取出itemPirce这个值时始终报:For input string: "${cart.itemPrice / 100}" 错误. 事故原 ...
- Java基础:类加载机制
之前的<java基础:内存模型>当中,我们大体了解了在java当中,不同类型的信息,都存放于java当中哪个部位当中,那么有了对于堆.栈.方法区.的基本理解以后,今天我们来好好剖析一下,j ...
- Python 插件杂谈 (1) ---- chardet
Python做为一门应用广泛,粉丝众多的语言.吸引了无数的开发者为其添砖加瓦.笔者使用python逾一个月,就接触了许多 python插件,写篇文章,仅做记录. 第一个介绍的是 chardet. ...
- Eclipse generate javadoc
注:若遇到导出文档乱码,则点击上图的[next]按钮,在vm options的输入框输入 -J-Xmx180m —- 设置内存大小 (若遇到内存溢出时) -encoding utf-8 ...
- js跨域请求实现
js代码 var url = zfba2url + "/fzyw/xzfy/smcl/autoUpdateByWS.action"; var data = { "writ ...