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.

  方法一:DFS  小数据AC, 大数据挂掉

class Solution {
public:
bool DFS(int A[],int n, int i)
{
if(i == n-) return true;
if(i >= n) return false;
int num = A[i];
if(num == ) return false;
for(int m = ; m <= num ;m++)
{
bool f = DFS(A,n,i+m);
if(f == true) return true;
} return false;
}
bool canJump(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n ==) return true;
return DFS(A,n,) ;
}
};

方法二: 还是大数据未过

class Solution {
public: bool canJump(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<bool> flag(n, false);
flag[] = true;
for(int i = ; i< n- ; i++)
{
if(flag[i] == false) continue;
int m = A[i];
for(int j =; j<= m && j+i< n ; j++)
flag[i+j] = true;
} return flag[n-] ;
}
};

上一份代码超时的主要原因是内循环,所以要设法改进内循环。改进的方法是不再设定每一个可能的位置为true,而是维护一个可以抵达的最远的距离maxJump。如果当前的i<=maxJump,则可以更新maxJump =

maxJump > i+A[i] ? maxJump : i+A[i]; 最后以maxJump > n-1来判断最后一个位置是否可达。 AC代码如下:

class Solution {
public:
bool canJump(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int maxJump = ;
for(int i = ; i< n- ; i++)
{
if(i <= maxJump)
maxJump = maxJump > A[i]+i ? maxJump : A[i]+i ; if(maxJump >= n-) return true;
} return maxJump >= n- ;
}
};

LeetCode_Jump Game的更多相关文章

  1. LeetCode_Jump Game II

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

随机推荐

  1. ZOJ 3188 ZOJ 3188 Treeland Exhibition(树形DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3278 题意:给出一棵树,找出一个不大于长度为m的链,使得其他点到该链 ...

  2. perl 对象 通过bless实现

    对象只是一种特殊的引用,它知道自己是和哪个类关联在一起的,而构造器知道如何创建那种关联关系. 这些构造器是通过使用bless操作符,将一个普通的引用物转换成一个对象实现的,

  3. Palindrome Partitioning 解答

    Question Given a string s, partition s such that every substring of the partition is a palindrome. R ...

  4. 关于qt学习的一点小记录(2)

    嗯...这次接了个单 要求图形界面,刚好可以巩固并学习下QT.毫不犹豫的就接了 下面记录下出现的问题: 1. QWidget和QDialog QDialog下的槽函数有accept()与reject( ...

  5. STS(Spring Tool Suite)使用前准备

    sts 的基础框架拿的eclipse的,你可以理解为eclipse + spring插件的高级升华版.在使用上可以很大限度的参考eclipse的操作. 首先,调整字体. 中文很麻烦的,因为编码问题.习 ...

  6. Linux shell编程 4 ---- shell中的循环

    1 for循环 1 for语句的结构 for variable in values; do statement done 2 for循环通常是用来处理一组值,这组值可以是任意的字符串的集合 3 for ...

  7. Android项目svn代码管理问题

    用svn控制版本,svn本身是不会识别哪些该传,哪些不该传,这就导致有些关于路径的东西(比如拓展jar的路径)也被上传了,而当别人下载后,那个路径对于这个人可能完全不存在,项目编译就会出问题.用ecl ...

  8. 几个Linux常见命令

    ls 作用:查看目录下的文件 格式:直接ls查看当前所在目录,或者  ls 参数 /目录名 参数: -l 查看详细信息 -a 显示隐藏文件 .   表示当前目录 ..  上级目录 -G 用不同颜色标记 ...

  9. MATLAB快速注释方法

    觉得有用,未免以后忘记,收藏了.原文来自:http://i.azpala.com/2008/09/18/matlab-multi-line-comment/ A. %{ 若干语句 %} B. 多行注释 ...

  10. php使用session来保存用户登录信息

    php使用session来保存用户登录信息 使用session保存页面登录信息 1.数据库连接配置页面:connectvars.php <?php //数据库的位置 define('DB_HOS ...