【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
.
class Solution {
public:
bool canJump(int A[], int n) {
int reach = ;
for (int i = ; i <= reach && i < n; ++i) {
reach = max(reach, A[i] + i);
if (reach >= n - ) {
return true;
}
}
return reach >= n - ;
}
};
用一个reach变量保存可以达到的最远位置,从前向后扫描不断地扩大reach的范围,若reach可以达到目标位置则返回true.
【Leetcode】Jump Game的更多相关文章
- 【LeetCode】Jump Game (一维动态规划 + 线性扫描)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【leetcode】Jump Game I & II (hard)
Jump Game (middle) Given an array of non-negative integers, you are initially positioned at the firs ...
- 【leetcode】 Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【leetcode】Jump Game I, II 跳跃游戏一和二
题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at the first ...
- 【LeetCode】Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- XE Button Color
XE Button Color,FMX Button 颜色 也可以放个rectangle+Glyph控件. http://blogs.embarcadero.com/sarinadupont/2014 ...
- nested exception is java.net.UnknownHostException: mybatis.org异常处理
最近自己写了个小项目(丛林商城V1.0),一个简单的网上商铺:主界面是商品的展示和登录,面对三种角色的人群:一般客户,VIP客户,管理员,与之对应的三种商品价格,登陆后根据具体角色来显示商品的价格:还 ...
- [Python Study Notes]堆叠柱状图绘制
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- checked多选,取消,反选
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- activity状态保存的bundl对象存放位置的思考
我们知道,当activity被异常终止时,可以把一些信息保存到bundle对象中,在下次启动时恢复. 那么,这个bundle对象是保存在哪里的呢? 这种状态保存的方法针对的是activity而不是进程 ...
- c++ 与 lua 简单交互参数介绍
原文http://blog.csdn.net/johnice/article/details/5517431 一.第一个例子 Hello World ! #include <stdio.h> ...
- function几种自执行的形式
1.(function(){})();这种是最常用的形式 2.var t = function(){}(); 3.-function(){}(); 这三种形式都能自执行
- tarjan进阶
一.边双连通分量 定义 若一个无向图中的去掉任意一条边都不会改变此图的连通性,即不存在桥,则称作边双连通图.一个无向图中的每一个极大边双连通子图称作此无向图的边双连通分量. 实际求法和强连通分量差不多 ...
- CH 4302 Interval GCD
辗转相减法的扩展 $gcd(x, y, z) = gcd(x, y - x, z - y)$ 当有n个数时也成立 所以构造$a_{i}$的差分数组$b_{i} = a_{i} - a_{i - 1}$ ...
- scau 2015寒假训练
并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...