面试经典 150 题 (十一)

class Solution {
    public int jump(int[] nums) {
        if (nums.length <= 1) return 0;
        //记录每次起跳所能跳到的最远的距离
        int farestIndex = 0;
        int maxIndex = 0;
        int start = 0;
        int times = 0;
        while(true){
            for (int i = start; i <= farestIndex; i++){
                if (maxIndex < (i + nums[i])){
                    maxIndex = i + nums[i];//记录当前条约范围内,能够调到最远距离的下标
                }
            }
            times++;
            start = farestIndex;
            farestIndex = maxIndex;
            maxIndex = 0;
            if (farestIndex >= nums.length - 1) break;
        }
        return times;

    }
}
posted @ 2024-02-05 17:45  破忒头头  阅读(16)  评论(0)    收藏  举报