45. Jump Game II
来源:互联网 发布:故宫软件 编辑:程序博客网 时间:2024/05/19 10:35
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 1
step from index 0 to 1, then 3
steps to the last index.)
Note:
You can assume that you can always reach the last index.
class Solution { public int jump(int[] nums) { if (nums.length == 1){ return 0; } int left = 0, right = 0; int num = 0, maxIndex = 0; while (true){ num ++; for (int i = left + 1; i < right; ++ i){ int tmp = i + nums[i]; if (maxIndex <= tmp){ maxIndex = tmp; } } maxIndex = Math.max(right + nums[right], maxIndex); if (maxIndex >= nums.length - 1){ return num; } left = right; right = maxIndex; } }}
阅读全文
0 0
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- 45. Jump Game II
- A
- 在LINUX如何用键盘复制,粘贴
- 循序渐进学java 13种核心技术
- Leetcode 41. First Missing Positive
- vue.js基础
- 45. Jump Game II
- Linux下各个目录的功能
- java异常
- vim常用指令
- 安吉尔高额
- Leetcode 20. Valid Parentheses
- struts2的简单搭建
- java虚拟机学习笔记2_垃圾收集器
- Oracle之组合查询和in有关的优化