LeetCode:Jump Game II
2016-06-15 16:42
411 查看
Jump Game II
Total Accepted: 65982 TotalSubmissions: 259300 Difficulty: Hard
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 =
[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.
Subscribe to see which companies asked this question
Hide Tags
Array Greedy
java code:
public class Solution { public int jump(int[] nums) { int currJumpMax = 0; // 当前可跳的最远距离 int lastJumpMax = 0; // 上一次可跳的最远距离 int stepCount = 0; // 步数 for(int i=0;i<nums.length-1;i++) { currJumpMax = Math.max(currJumpMax, i + nums[i]); if(i==lastJumpMax) { //到了上一次可跳的距离才可更新步数 stepCount++; lastJumpMax = currJumpMax; } } return stepCount; } }
相关文章推荐
- Android ThreadUtil 线程公共类,判断是否在主线程/ 子线程执行 相关操作
- verilog中always块延时总结
- count-numbers-with-unique-digits
- Precision and Recall
- scanf和cin的区别
- MAC平台下Xcode配置使用OpenCV的具体方法 (2016最新)
- css盒子模型
- 聚合索引和非聚合索引简单介绍
- 图片内存复制性能对比
- 什么是浮动IP
- SqlServer调用CLR服务
- 上田雅美是个 4000 汉子,心然的那首神曲竟然出自reset
- JSON简介
- 图片上传出现的问题
- java nio中buffer缓冲区一些方法
- ZBrush中的皮肤纹理是怎么雕刻出来的
- spring框架的项目报错:No mapping found for HTTP request with URI [ ] in DispatcherServlet with name 'spring'
- 获取String中的所有数字并转化为int类型
- JavaScript专题(二):深入理解iframe
- python lib error