LeetCode 45 Jump Game II
2016-05-20 15:59
411 查看
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.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A =
The minimum number of jumps to reach the last index is
from index 0 to 1, then
Note:
You can assume that you can always reach the last index.
贪心法:
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A =
[2,3,1,1,4]
The minimum number of jumps to reach the last index is
2. (Jump
1step
from index 0 to 1, then
3steps to the last index.)
Note:
You can assume that you can always reach the last index.
贪心法:
public int jump(int[] nums) { int reach = 0;//最右能跳到哪里 int last = 0;//上次落的的最大号阶梯,如果当前超过了,那必然是step了一下 int step = 0;//步数 for (int i = 0; i < nums.length; i++) { if (i > last) { step++; last = reach; } reach = Math.max(reach, i + nums[i]); } return step; }
相关文章推荐
- In House打包流程
- ObjectInputStream序列化反序列化
- VC枚举系统所有字体
- 制作自己的镜像(一)
- 给dnf添加代理
- 六十二 Web开发 使用模板
- [转]使用Enumeration和Iterator遍历集合类
- localhost不能访问127.0.0.1可以访问的原因及解决方法 被打磨的不像人样
- modelsim编译vivado ip报错error protect region
- <根据马士兵>动态加载过程
- 论ORACLE 9i 的体系图为什么这么金典
- 文件与IO(一)之字节流
- mysql新建用户表,赋权,远程登陆
- hdu4707 Pet
- nginx学习笔记六(Nginx事件框架处理流程)
- 菜鸟从零开始初始WebSocket -Server篇(二)
- php抓取网页上的指定内容
- Android深入浅出自定义控件(一)
- spring-boot学习笔记
- sql注入分类