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. hdu 3339 In Action

    http://acm.hdu.edu.cn/showproblem.php?pid=3339 这道题就是dijkstra+01背包,先求一遍最短路,再用01背包求. #include <cstd ...

  2. csdn的下载链接token

    qt之QComboBox定制 http://www.cnblogs.com/swarmbees/p/5710714.html http://download.csdn.net/detail/qq_30 ...

  3. GBT28181中的RTP

    国标中说h264数据按照RFC3984打包,但是国标的测试工具——SPVMN,却不支持RFC3984的打包方式.无奈之下直接用RFC3550的方式打包,其实就是分包,然后加上RTP头,对于一帧的结束, ...

  4. SoftLayerDebug

  5. UNIX时间戳与日期的相互转换

    mysql中UNIX时间戳与日期的相互转换 UNIX时间戳转换为日期用函数:FROM_UNIXTIME() select FROM_UNIXTIME(1410318106); 日期转换为UNIX时间戳 ...

  6. WPF最基本的4个引用

    Windowsbase Windows基本类库 PresentationCore wpf核心类库 PresentationFramework wpf框架 System.Axml 系统类库

  7. python 得到一个元素的所有下标(网友提供:http://www.oschina.net/code/snippet_212212_38917)

    def all_index(l,o): def find_index(l,o,start=0): try: index=l.index(o,start) except: index=-1 return ...

  8. c++之 常量

    const常量 当在类型名前面加上关键字const后,表示它是一个只读的量,不能对其进行修改,因而被称为常量. 下面的例子对常量进行修改: const常量是只读的,可以读取它的值,或者用printf打 ...

  9. [CSS3] CSS Display Property: Block, Inline-Block, and Inline

    Understanding the most common CSS display types of block, inline-block, and inline will allow you to ...

  10. sqlite3安装

    SQLite命令行程序(CLP)是开始使用SQLite的最好选择,按照如下步骤获取CLP: 1).打开浏览器进入SQLite主页,www.sqlite.org. 2).单击页面顶部的下载链接(Down ...