LeetCode 45. Jump Game II

双指针 On遍历一遍就好

找出当前步数能到达的最远距离,直到距离覆盖了目标点

class Solution {
    public int jump(int[] nums) {
        int n = nums.length;
        int l = 0;
        int r = 0;
        int ans = 0;
        while(l<=r){
            if(r>=n-1)
                return ans;
            ans++;
            int nxt = -1;
            for(int i=l;i<=r;i++)nxt = Math.max(nxt,i+nums[i]);
            l = r+1;
            r = nxt;
        }
        return ans;
    }
}

猜你喜欢

转载自blog.csdn.net/Dale_zero/article/details/121555413