LeetCode45-跳跃游戏 II
题目
给定一个非负整数数组,你最初位于数组的第一个位置。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
你的目标是使用最少的跳跃次数到达数组的最后一个位置。
输入: [2,3,1,1,4]
输出: 2
解释: 跳到最后一个位置的最小跳跃数是 2。
从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
假设你总是可以到达数组的最后一个位置。
解法
class Solution {
public int jump(int[] nums) {
int n = nums.length;
int end = 0;
int maxPos = 0;
int ans = 0;
for(int i = 0 ; i < n - 1 ; ++i){
maxPos = Math.max(nums[i]+i,maxPos);
//每次抵达end之后,将此时可以到达的最远距离设为end
if(i==end){
end = maxPos;
++ans;
}
}
return ans;
}
}