【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 ...
随机推荐
- ORA-04098 trigger 'DBBJ.DB_EV_ALTER_ST_METADATA' is invalid and failed re-validation
转自:https://blog.csdn.net/dragoo1/article/details/9411105 ORA-04098: trigger 'DBBJ.DB_EV_ALTER_ST_MET ...
- pl/sql使用部分整理
在工作中使用pl/sql工具,总结一下常用操作,以便以后复习,也希望帮助朋友们熟悉pl/sql操作! 1.pl/sql记住登录密码PL/SQL Developer->tools->Pref ...
- StackMapTable format error
环境:Oracle Java 7 , Mac OSX 报错如上图所示,主要是 Caused by: java.lang.ClassFormatError: StackMapTable format e ...
- 每天一道算法题(24)——自定义幂函数pow
double myPower(double base, int exponent){ if(exponent==0) return 1; if(exponent==1) return base; if ...
- 数据库理论-范式(1NF、2NF、3NF)
范式是“符合某一种级别的关系模式的集合,表示一个关系内部各属性之间的联系的合理化程度”. 第一范式(1NF)是指数据库表的每一列都是不可分割的基本数据项.(每个属性不可分割)第二范式(2NF)要求数据 ...
- ROS Learning-013 beginner_Tutorials (编程) 编写ROS服务版的Hello World程序(Python版)
ROS Indigo beginner_Tutorials-12 编写ROS服务版的Hello World程序(Python版) 我使用的虚拟机软件:VMware Workstation 11 使用的 ...
- QGraphicsScene绘制网格背景
博客转载自:https://blog.csdn.net/u010177010/article/details/51496038 //两条轴线QPolygonF myPolygon1; myPolygo ...
- PCL—点云滤波(基于点云频率) 低层次点云处理
博客转载自:http://www.cnblogs.com/ironstark/p/5010771.html 1.点云的频率 今天在阅读分割有关的文献时,惊喜的发现,点云和图像一样,有可能也存在频率的概 ...
- Mind Map - FreeMind
FreeMind[1]是一款基于java的免费的脑图(mind mapping)制作与管理软件.FreeMind开发项目组正致力于使其成为一款高效率的工具.FreeMind具有一键“展开/折叠”功能以 ...
- Java Swing 创建转圈的进度提示框
Java Swing 创建转圈的进度提示框 摘自 https://blog.csdn.net/nihaoqiulinhe/article/details/52439486 置顶2016年09月05日 ...