经典算法——Jump Game
2016-04-08 09:45
363 查看
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 =
A =
Subscribe to see which companies asked this question
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.
Subscribe to see which companies asked this question
class Solution { public: bool canJump(vector<int>& nums) { int maxreach = 0; for (int i = 0; i<nums.size() && i <= maxreach; ++i) { maxreach = max(i + nums[i], maxreach); if (maxreach >= nums.size() - 1)//当我们知道最远可以达到数组最末尾时,可以直接返回true,而不用遍历整个数组 return true; } } };
相关文章推荐
- 指针和二级指针的区别
- Android学习(6) -- 电话拨号器
- Android学习(7) -- 点击事件的四种写法
- 曾经经历的面试题汇总(1)
- 解决磊科随身wifi在ubuntu 14.04 下频繁掉线且信号弱的问题
- Oracle 自己主动诊断资料档案库 (ADR)、自己主动诊断工作流、ADRCI工具
- Struts——Action与ActionSupport
- java执行sql语句一直没有响应
- Android自动检测版本及自动升级
- 安装LR11 时,安装Microsoft Visual c++2005 sp1运行时组件,就会提示命令行选项语法错误,键入“命令/?”可获取帮肋信息
- Linux dos2unix --将DOS格式文本文件转换成UNIX格式
- 配置Linux内核源码make menuconfig 时unable to find the ncurses libraries 错误
- 先做事再做人
- 线程安全和线程不安全理解
- 通过IL分析C#中的委托、事件、Func、Action、Predicate之间的区别与联系
- HTML转义字符大全(转)
- 在ORACLE中给表、列增加注释以及读取注释
- Android错误集锦
- 如何用NTFS For Mac修改Mac上ntfs参数
- ExtJS实现Excel导出