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. Ubuntu mysql中文乱码解决

    #vim /etc/mysql/my.cnf 找到[mysqld]添加 character-set-server = utf8 重启mysql #restart mysql mysql> sho ...

  2. idea

    一. 常用快捷键 搜索class Ctrl+N 搜索文件 Ctrl+Alt+N 当前窗口查找/全工程查找 Ctrl+F/Ctrl+Shift+F,F3/Shift+F3前后移动 上/下一个位置 Ctr ...

  3. 2013第50周二eclipse工具尝试

    今天更深入的认识了eclipse开发工具,出现了各种问题在网络的帮助下最终都解决了,感觉似乎明白了很多道理需要总结一下,现在发现晚了,那就先记录下我印象深的几个问题吧: 1.eclipse编辑器设置. ...

  4. 快速批量导入庞大数据到SQL SERVER数据库(ADO.NET)

    原文地址:http://www.cnblogs.com/chenxizhang/archive/2008/11/11/1331060.html 如果你需要在程序中批量插入成千上万行的数据,你会怎么编写 ...

  5. hdu 1506 Largest Rectangle in a Histogram(单调栈)

                                                                                                       L ...

  6. Linux常用命令及使用技巧

    本文重点讲述Linux命令的使用,命令是学习Linux必须熟练掌握的一个部分.Linux下的命令大概有600个,而常用的命令其实只有80个左右,这些常用的命令是需要灵活掌握的.虽然Linux的各个发行 ...

  7. VC调试笔记

    1.windows-32调试: ①使用map文件根据崩溃地址寻找对应的源代码文件和行号 勾选project->settings->link->General mapfile,对应的P ...

  8. WeiFenLuo.winFormsUI.Docking.dll的使用(停靠效果)

    1.  重置工具箱: 新建一个WinForm程序,项目名称为TestDockPanelControl.选中Form1窗体后选择工具箱--->>新建个添加选项卡命名为WeiFenLuo--- ...

  9. Android布局自定义Shap圆形ImageView,可以单独设置背景与图片

    一.图片预览:                  一.实现功能: 需求要实现布局中为圆形图片,图片背景与图标分开且合并到一个ImageView. 二.具体实现: XML中布局中定义ImageView, ...

  10. TransactionScrope 2

    继上一篇文章TransactionScrope 在做相应的变动时,发现可以重现ORA-14450错误,如: List<Thread> ls = new List<Thread> ...