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 ...
随机推荐
- 那两年炼就的Android内功修养
http://blog.csdn.net/luoshengyang/article/details/8923485 http://iconsparadise.com/ http://blog.csdn ...
- elasticsearch 性能测试
最近花很大的经历来做性能测试,把结果整理到了ppt中,可能有个别地方不准,但是可以看看一个趋势. 主要分为两部分,一部分是写入elasticsearch性能,一部分是查询测试,elasticsearc ...
- 【转】Android兼容性测试CTS --环境搭建、测试执行、结果分析
原文网址:http://www.cnblogs.com/zh-ya-jing/p/4396918.html 为了确保Android应用能够在所有兼容Android的设备上正确运行,并且保持相似的用户体 ...
- tmux 配置
tmux配置文件名为.tmux.conf,位于用户根目录下. 常用的配置为: # vimsetw -g mode-keys vibind [ copy-modebind -t vi-copy v be ...
- vb串口通信界面
界面如上: 程序如下: Dim num As Byte '申明一个全局变量为单字节型 '单击“清空接收缓冲区”按钮时,将接收缓冲区清空,此过程为“清空接收缓冲区”的单击事件 Private S ...
- Grid++Report 数据填充教程
用 Grid++Report的报表设计器应用程序设计一个简单的报表:“机房开发收入总汇表” 一.定义报表头 1.执行菜单命令“插入”→“报表头” 2.执行菜单命令“插 ...
- 【POJ】Buy Tickets(线段树)
点更新用法很巧妙的一道题.倒序很容易的找到该人的位置,而update操作中需要不断的变化下标才能合理的插入.看了别人写的代码,学习了. #include <iostream> #inclu ...
- fcitx 输入框纵向
打开~/.config/fcitx/conf/fcitx-classic-ui.config 找到下面的:# 竖排候选词列表# 可选值:# True False#VerticalList=True-- ...
- How to uninstall (remove) JAVA from OS X Lion
Open terminal (Applications -> Utilities -> Terminal) To remove JVM enter folowing: sudo rm -r ...
- 计划任务中使用NT AUTHORITY\SYSTEM用户和普通管理员用户有什么差别
原文地址:http://www.ynufe.edu.cn/metc/Article/ShowArticle.asp?ArticleID=805 系统管理员会碰到这种问题,为什么在更改系统登录用户pas ...