【LeetCode】55. 跳跃游戏

55. 跳跃游戏

解法: 贪心算法

记录当前位置可以走的最远位置,若能达到最后位置,即可到达

public boolean canJump(int[] nums) {
    if (nums == null || nums.length <= 1)
        return true;

    int n = nums.length - 1;
    int max = nums[0];

    for (int i = 1; i < nums.length; i++) {
        if (max >= n)
            return true;
        if (max >= i) {
            max = Math.max(max, nums[i] + i);
        }
    }
    return false;
}
posted @ 2025-09-25 18:29  WilsonPan  阅读(5)  评论(0)    收藏  举报