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: ??? ...
随机推荐
- TCP Socket Port Check
写了两个小程序,主要是用于linux和windows下TCP端口的检测,自带的telnet无法满足我批量检测的需要,在我眼里这类端口检测程序最为关键的是超时的限制,若端口不能却要老久才返回结果,有点不 ...
- Linux磁盘的性能详细测试办法
# 测试写入20Gb文件sync && time -p bash -c "(dd if=/dev/zero of=test.dd bs=1000K count=20000; ...
- 64位直接加载个img 标签的src
- ubuntu16.04下安装docker
原文地址:传送门 本文开发环境为Ubuntu 16.04 LTS 64位系统,通过apt的docker官方源安装最新的Docker CE(Community Edition),即Docker社区版,是 ...
- 六十五 async/await
用asyncio提供的@asyncio.coroutine可以把一个generator标记为coroutine类型,然后在coroutine内部用yield from调用另一个coroutine实现异 ...
- OpenStack 认证服务 KeyStone连接和用户管理(五)
一) 创建环境变量链接keyston vim adminrc export OS_USERNAME=admin export OS_PASSWORD=redhat export OS_PROJECT_ ...
- HDU 1097.A hard puzzle-快速幂/取模
快速幂: 代码: ll pow_mod(ll a,ll b){ ll ans=; while(b){ ==){ ans=ans*a%mo ...
- (5) go 控制台输入输出、进制转换、原反补码、位运算
一.控制台接受输入 二.原反补码 三.位运算 四.移位运算
- 洛谷——P1890 gcd区间
P1890 gcd区间 题目描述 给定一行n个正整数a[1]..a[n]. m次询问,每次询问给定一个区间[L,R],输出a[L]..a[R]的最大公因数. 输入输出格式 输入格式: 第一行两个整数n ...
- webpack 打包过程及常用插件
前言 要理解webpack 首先明白三个概念:module,chunk,bundles,即输入,中间态,输出. chunk: This webpack-specific term is uesd in ...