import java.util.*;
public class Solution {
public int Jump (int len, int[] nums) {
// 初始化
int curMax = 0; // 当前能到达的最远位置
int nextMax = nums[0]; // 下一跳能到达的最远位置
int step = 0; // 跳跃步数
// 贪心算法
for (int i = 0; i < len-1; i++) {
// 更新下一跳能到达的最远位置
nextMax = (i + nums[i]) > nextMax ? (i + nums[i]) : nextMax;
// 到达当前能到达的最远位置,则进行跳跃并更新边界
if (i == curMax) {
step++;
curMax = nextMax;
}
}
// 返回结果
return step;
}
}



京公网安备 11010502036488号