45. 跳跃游戏 II
给你一个非负整数数组 nums ,你最初位于数组的第一个位置。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
你的目标是使用最少的跳跃次数到达数组的最后一个位置。
假设你总是可以到达数组的最后一个位置。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/jump-game-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public int jump(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int ret = 0;
int curMax = 0, nextMax = 0;
for (int i = 0; i < nums.length; ++i) {
if (i > curMax) {
ret++;
curMax = nextMax;
}
nextMax = Math.max(nextMax, i + nums[i]);
}
return ret;
}
}
心之所向,素履以往 生如逆旅,一苇以航

浙公网安备 33010602011771号