1、题目来源
45. 跳跃游戏 II - 力扣(LeetCode)
2、题目描述
给定一个长度为 n
的 0 索引整数数组 nums
。初始位置为 nums[0]
。
每个元素 nums[i]
表示从索引 i
向前跳转的最大长度。换句话说,如果你在 nums[i]
处,你可以跳转到任意 nums[i + j]
处:
0 <= j <= nums[i]
i + j < n
返回到达 nums[n - 1]
的最小跳跃次数。生成的测试用例可以到达 nums[n - 1]
。
示例 1:
输入: nums = [2,3,1,1,4] 输出: 2 解释: 跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3步到达数组的最后一个位置。
示例 2:
输入: nums = [2,3,0,1,4] 输出: 2
提示:
1 <= nums.length <= 104
0 <= nums[i] <= 1000
- 题目保证可以到达
nums[n-1]
3、题解分享
class Solution {public int jump(int[] nums) {// 思路:贪心 + 使用变量记录 当前步能跳到的最远距离,再用另一个变量记录 下一步能跳到的最远距离,每次跳出当前步的最远距离就++ansint n = nums.length;int ans = 0;int rightMaxIndex = 0;int nextIndex = 0;for(int i = 0;i<n;++i){if(i > rightMaxIndex){++ans;rightMaxIndex = nextIndex;}nextIndex = Math.max(nextIndex,nums[i]+i);}return ans;}
}