Jump Game
来源:互联网 发布:发票数据导出软件 编辑:程序博客网 时间:2024/06/06 01:11
1.题目
给出一个非负整数数组,你最初定位在数组的第一个位置。 数组中的每个元素代表你在那个位置可以跳跃的最大长度。 判断你是否能到达数组的最后一个位置。
A = [2,3,1,1,4],返回 true.
A = [3,2,1,0,4],返回 false.
2.算法
我们维护两个变量,一个是从前一步出发能够到达的最远距离,一个是从当前出发能够到达的最远距离
public boolean canJump(int[] a) { // wirte your code here if (a == null || a.length == 0) { return false; } int reach = 0; for (int i = 0; i < a.length && i <= reach; i++) { reach = Math.max(i + a[i], reach); } if (reach < a.length - 1) { return false; } return true; }
原文连接
点击打开链接
0 0
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- 关于操作系统中进程、线程、和任务之间的关系
- JDK版本报错:override a superclass method和java compiler level does not match the version
- OC代码规范总结
- 一些常用的CSS兼容性写法
- Docker
- Jump Game
- 谈谈fresco的bitmap内存分配
- 水题 模板
- sed命令详解
- Nginx源码分析 - Nginx启动以及IOCP模型
- linux shell
- bzoj4383
- 【Linux】将Oracle安装目录从根目录下迁移到逻辑卷
- 2016新财富最佳分析师榜单全揭晓