45. 跳跃游戏 II
45. 跳跃游戏 II - 力扣(LeetCode)
class Solution {public int jump(int[] nums) {int pos = nums.length - 1;//从后往前int ans = 0;//记录跳跃的次数while (pos > 0){//贪心的思想,找能跳到当前位置的距离最远的i,从左往右遍历for (int i = 0; i < pos; i++) {if (i + nums[i] >= pos){pos = i;ans++;break;//找到了能跳到当前位置的距离最远的i,跳出本次循环}}}return ans;}
}