LeetCode 第四十五题(Jump Game II)Java
来源:互联网 发布:淘宝直通车掌柜热卖 编辑:程序博客网 时间:2024/06/06 14:09
原题:
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.)
思路:
用一个变量curRch记录下当前步数最远能走到哪里;
用一个变量curMax依次遍历,得到以某一位置下能到达的最远位置;
如果遍历到 i 位置,curRch到达不了的话,说明此时要多走一步到 i 位置,并更新curRch;
记录步数并返回;
代码:
public class Solution { public int jump(int[] nums) { int Length=nums.length; int times=0; int curRch=0; int curMax=0; for(int i=0;i<Length;i++){ if(curRch<i){ times++; curRch=curMax; } curMax=Math.max(curMax,nums[i]+i); } return times; }}
0 0
- LeetCode 第四十五题(Jump Game II)Java
- [Leetcode] Jump Game II (Java)
- [LeetCode][Java] Jump Game II
- leetcode:Jump Game II 【Java】
- Jump Game II leetcode java
- LeetCode: Jump Game II
- LeetCode Jump Game II
- LeetCode: Jump Game II
- [Leetcode] Jump Game II
- [LeetCode] Jump Game II
- [Leetcode] Jump Game ii
- leetcode Jump Game II
- leetcode Jump Game II
- [LeetCode]Jump Game II
- [leetcode]Jump Game II
- LeetCode-Jump Game II
- [leetcode] Jump Game II
- LeetCode - Jump Game II
- BOM(byte-order mark):字节顺序标记
- JDBC
- 第十周 小学生百以内算术考试系统
- nyoj 303 序号互换 第四届河南省赛
- C++_STL_数据结构_stack_栈
- LeetCode 第四十五题(Jump Game II)Java
- hashmap 死循环解析
- python signal handling
- 通知的各种实用写法技巧
- 数据存取之SQLite浅析
- 创建类的对象
- Flip Game(DFS枚举)
- HQL时间段查询
- Android中五中存储方式的区别和优劣性