Leet code —Jump Game
2015-10-14 14:12
267 查看
问题叙述性说明:
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.
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) { if (n < 1) return false; int max_step, i, tmp; max_step = tmp = 0; for (i = 0; i < n; i++) { if (A[i] > max_step) max_step = A[i]; if ((tmp = max_step + i) == n-1 || tmp > n-1) return true; if (max_step == 0) return false; max_step--; } } };
相关文章推荐
- IBM WebSphere MQ 7.5基本用法
- centos下安装bugfree3.0.4
- Maximum Depth of Binary Tree
- GCD简介
- Android 数据查询query函数参数解析
- 农村宅基地相关政策
- javascript caller/callee
- python 编码问题
- MySQL查看、创建和删除索引的方法
- Redis主从复制和主从切换
- 【009】阅读代码
- 批处理方法
- HibernateDaoSupport类的使用
- linux目录跳转快捷方式——z武器
- Launcher问题解析
- 将画布(canvas)图像保存成本地图片的方法
- Integer与int的种种比较你知道多少?
- 我的leveldb总结
- 三个重要的散列演示
- SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)