代码【贪心】https://leetcode-cn.com/problems/jump-game-ii/
思路:
1、设定起始点start=0与结束点end=nums[0]
2、贪心法寻找可跳到的最远距离
public int jump(int[] nums) {
int n = nums.length;
if (n == 1) {
return 0;
}
int start = 0;
int end = nums[0];
int step = 1;
while (end < n - 1) {
int maxi = 0; // 记录最大潜力值
for (int i = start; i < end 1; i ) { // 扫描区间,更新潜力值
if (i nums[i] > maxi) {
maxi = i nums[i];
}
}
// 进入下一步扫描
start = end 1;
end = maxi;
step = 1;
}
return step;
}
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved