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. 理解JWT(Json Web Token)

    这篇文章写得不错: 理解JWT(JSON Web Token)认证及python实践,这里不做转载,仅摘要如下,有删改,仅做个人学习,感谢原作者. 常用认证机制: 1)HTTP basic Auth: ...

  2. jquery实现页面加载时删除特定class 的div内前三个字符

    jQuery(document).ready(function(){        jQuery("div.groupheader").each(function(){ $(thi ...

  3. Vue组件之props,$emit与$on以及slot分发

    组件实例之间的作用域是孤立存在,要让它们之间的信息互通,就必须采用组件的通信方式  props用于父组件向子组件传达信息 1.静态方式 eg: <body> <div id=&quo ...

  4. cocos2dx 大地图分块加载的研究(初)

    http://blog.csdn.net/dinko321/article/details/46739563 http://blog.csdn.net/u012812482/article/detai ...

  5. 解决Cocos2d-js 在使用 TiledMap时的黑线问题

    在项目中,加载TiledMap时,如果当前显示分辨率与设计分辨率不符,做出的地图上会有黑线产生.屏幕移动时,也会有黑线. 解决的方式很简单.找到配置文件  CCConfig.js  一般情况是在 ra ...

  6. CodeForces 738E Subordinates

    排序,构造. 相当于告诉我们一棵树$n$个节点,每个节点在哪一层,至少需要移动多少个节点,才能让这些节点变成一棵树. 按照层次排个序移动一下就可以了,优先选择那些不是$s$但是层次是$0$的节点,如果 ...

  7. 【我要学python】面对对象编程之继承和多态

    class animal(object): def run(): print('animal is running...') class dog(animal): def run(self): pri ...

  8. HZAU 1203 One Stroke(倍增)

    题目链接:http://acm.hzau.edu.cn/problem.php?id=1203 [题意]给你一颗完全二叉树每个节点都有一个权值,然后要你从上往下找一条链,值得链上权值的和<K,且 ...

  9. ALL运算符

    ALL在英文中的意思是“所有”,ALL运算符要求比较的值需要匹配子查询中的所有值.ALL运算符同样不能单独使用,必须和比较运算符共同使用. 下面的SQL语句用来检索在所有会员入会之前出版的图书: SE ...

  10. 【分块】计蒜客17120 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 G. Xor

    题意:给一棵树,每个点有权值.q次询问a,b,k,问你从a点到b点,每次跳距离k,权值的异或和? 预处理每个点往其根节点的路径上隔1~sqrt(n)的距离的异或和,然后把询问拆成a->lca(a ...