跳跃游戏 II (Jump Game II)

 

思路:

// @Title: 跳跃游戏 II (Jump Game II)
// @Author: qisiii
// @Date: 2024-05-10 18:53:32
// @Runtime: 1 ms
// @Memory: 43.9 MB
// @comment: 
// @flag: 
class Solution {
    public int jump(int[] nums) {
        int p=0,step=0;
        while(p<nums.length-1){
            if(p+nums[p]>=nums.length-1){
                return step+1;
            }
            int max=0,index=p;
            for(int i=p;i<=p+nums[p];i++){
                if(i+nums[i]>max){
                    max=i+nums[i];
                    index=i;
                }
            }
            p=index;
            step++;
        }
        return step;
    }
}

+++ title = “跳跃游戏 II (Jump Game II)” draft = false +++

思路:

// @Title: 跳跃游戏 II (Jump Game II)
// @Author: qisiii
// @Date: 2024-09-22 18:25:00
// @Runtime: 1 ms
// @Memory: 44.2 MB
// @comment: 
// @flag: 
class Solution {
    public int jump(int[] nums) {
        int index = 0;
        int curMax = nums[0];
        int nextMax = 0;
        int count = 0;
        //当前指针没有走到最后的时候
        while (index < nums.length-1) {
            //在当前可活动范围寻找最大可跳跃地点
            while (index <= nums.length-1&&index <= curMax) {
                nextMax = Math.max(nextMax, index + nums[index]);
                index++;
            }
            index = curMax;
            curMax = nextMax;
            count++;
            
        }
        return count;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18