LeetCode_Jump Game
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的更多相关文章
- LeetCode_Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- Oracle left查询案例
)) summoney from( select t2.ano,d.dmoney from ( select t1.*,c.cno from( select a.ano,b.bno from t_a ...
- Uubntu14.04 LST安装NodeJS
1:从官网下载node.js源码http://nodejs.org/download/ 当前最新版为node-v0.10.33 2:安装 $ tar zxvf node-v0.10.33.tar.gz ...
- mysql 增量导入到elasticsearch
<pre name="code" class="html">zjtest7-redis:/odbc_es# cat /odbc_es/run_mys ...
- 【转】【Android】HAL分析
原文网址:http://www.cnblogs.com/lcw/p/3335505.html HAL概述 以下是基于android4.0.3,对应其他低版本的代码,可能有所差异,但基本大同小异. An ...
- 链表的基本操作(Basic Operations on a Linked List)
链表可以进行如下操作: 创建新链表 增加新元素 遍历链表 打印链表 下面定义了对应以上操作的基本函数. 创建新链表 新链表创建之后里面并没有任何元素,我们要为数据在内存中分配节点,再将节点插入链表.由 ...
- 【HDU1198】Farm Irrigation(回溯+记忆化搜索)
数据流小,深搜即可.有些暴力.看其他人的题解用二维转换成一维做的并查集很巧妙,马上去研究一下!! #include <iostream> #include <cstring> ...
- Linux 程序设计的一些优化措施
Linux 程序设计的一些优化措施 这些知识是在平常的阅读中,零散的获得的,自己总结了一下,分享在这里 全局变量VS函数参数 全局变量在Linux下的驱动编程里边,用的是非常多,例如中断服务函数ISR ...
- linux下部署svn服务器
系统Linux debian 2.6.32-5-686 先安装svn工具:apt-get install subversion,耐心等待安装完成.安装完成后svn客户端.服务器都有了. 接者建立svn ...
- pyqt menu子级方向例子学习
代码和UI文件:http://yunpan.cn/QCkXbX8mnSNke(提取码:51e1) 图片如: 代码如下: from PyQt4 import QtCore,QtGui,Qt import ...
- Struts2+Spring+Hibernate 三大框架的合并集成
这次来看看Struts2+Spring+Hibernate三大框架的整合应用,主要是Spring和Hibernate框架的整合,因为前边已经将Strtus2+Spring整合过了基本一样. 首先看一 ...