leetcode 0045
说明
只是为了记录一下,不求多快,也不深究。
会简要描述思路,代码中不写注释。
如碰到不会做的用了别人代码会在博客中标出。
题目描述
参考
参考了B站up主: WEAK-CHICKEN 的讲解,链接如下:
思路
这题没做出来不应该的,没耐心想了,就是一个贪心。
记录当前区域内能到达的最远index,遍历完当前区域后,更新区域,step++。
class Solution {
public int jump(int[] nums) {
if (nums.length == 1) {
return 0;
}
int step = 1;
int small = 1;
int big = nums[0];
while (big < nums.length - 1) {
int tempBig = big;
for (int j = small; j <= tempBig; j++) {
if (j > nums.length - 1 || nums[j] + j > nums.length - 1) {
return step + 1;
} else {
if (j + nums[j] > big) {
big = j + nums[j];
}
}
}
step++;
small = tempBig + 1;
}
return step;
}
}