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的更多相关文章

  1. [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 ...

  2. [LeetCode] Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. [LeetCode] Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. Leetcode 45. Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. Leetcode 55. Jump Game

    我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...

  6. LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  7. Leetcode jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. Leetcode jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. 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: ??? ...

随机推荐

  1. NYOJ 116 士兵杀敌二

    士兵杀敌(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5   描述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的军师,南将军经常 ...

  2. 调用git命令行执行更新的思路

    cd /usr/local/software/CloudPlatformUtil/GitLab # CentOS6.5自带的git版本是1.7.1 # 安装高版本git wget -O git.zip ...

  3. C++使用第三方静态库的方法

    以jasoncpp为例 首先在github下载最新的的jasoncpp包并解压 找到类似makefiles的文件编译,一般是打开sln文件使用vs编译(生成解决方案),在build文件中可以找到对应的 ...

  4. 部署centos6

    挂载镜像和导入镜像 mount /dev/cdrom /media ll /media/ cobbler import --path=/media --name=centos6.5--arch=x86 ...

  5. 2.Spark Streaming运行机制和架构

    1 解密Spark Streaming运行机制 上节课我们谈到了技术界的寻龙点穴.这就像过去的风水一样,每个领域都有自己的龙脉,Spark就是龙脉之所在,它的龙穴或者关键点就是SparkStreami ...

  6. Centos的 mysql for python的下载与安装

    mysql-python的安装包下载地址:http://sourceforge.net/projects/mysql-python/files/latest/download linux环境是 Cen ...

  7. POJ 1089 Intervals【合并n个区间/贪心】

    There is given the series of n closed intervals [ai; bi], where i=1,2,...,n. The sum of those interv ...

  8. 并发系列4-大白话聊聊Java并发面试问题之公平锁与非公平锁是啥?【石杉的架构笔记】

  9. java 反射 获取Class对象的三种方式

    String str="hello world"; //方式一 :通过对象的getClass()方法 Class<?> clazz1 =str.getClass(); ...

  10. centos 7 的安全检查和ip封锁设置

    查看最近登录失败的验证记录 tail -f grep "authentication failure;" /var/log/secure 发现有个ip频繁尝试登录, /sbin/i ...