[LintCode] 跳跃游戏II Jump Game II
2016-04-11 15:13
253 查看
给出一个非负整数数组,你最初定位在数组的第一个位置。
数组中的每个元素代表你在那个位置可以跳跃的最大长度。
你的目标是使用最少的跳跃次数到达数组的最后一个位置。
样例
给出数组A = [2,3,1,1,4],最少到达数组最后一个位置的跳跃次数是2(从数组下标0跳一步到数组下标1,然后跳3步到数组的最后一个位置,一共跳跃2次)
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.
Example
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
数组中的每个元素代表你在那个位置可以跳跃的最大长度。
你的目标是使用最少的跳跃次数到达数组的最后一个位置。
样例
给出数组A = [2,3,1,1,4],最少到达数组最后一个位置的跳跃次数是2(从数组下标0跳一步到数组下标1,然后跳3步到数组的最后一个位置,一共跳跃2次)
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.
Example
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
public class Solution { /** * @param A: A list of lists of integers * @return: An integer */ public int jump(int[] A) { if(null == A || A.length <= 1) return 0; int max = 0, i = 0; int count = 0; while(max < A.length - 1) { if(i+A[i] > max) { max = i+A[i]; count++; } i++; } return count; } }
相关文章推荐
- eclipse如何查看jar包源码
- iOS学习笔记06-手势识别
- iOS学习笔记07-运动事件和远程控制
- 【剑指offer系列】 n个骰子的点数___43
- Windows 钩子技术详解
- SqlServer发送邮件
- JavaScript 闭包
- C# 操作配置文件
- Linux 下报错:A Java RunTime Environment (JRE) or Java Development Kit (JDK) must解决方案
- Spring+Struts2+Mybatis
- Spring整合hibernate4:事务管理
- 科技网站
- GOT表,PLT表,代码段重定位,数据段重定位--Linux动态连接原理
- Android 打造万能圆点指示器
- iOS学习笔记05-触摸事件
- C和指针详解------结构和联合
- IDEA 新建文件默认加入CVS
- Android Studio debug使用release的签名(微信分享)
- AspectJ本质剖析
- iOS学习笔记04-视图切换