55. 跳跃游戏

2021-08-12  本文已影响0人  justonemoretry
image.png

解法

class Solution {
    public boolean canJump(int[] nums) {
        int len = nums.length;
        // 当前能到最大下标
        int maxIndex = 0;
        for (int i = 0; i < len; i++) {
            // i小于等于最大下标,就可以继续往后跳,不然证明当前点到不了
            if (i <= maxIndex) {
                maxIndex = Math.max(i + nums[i], maxIndex);
                if (maxIndex >= len - 1) {
                    return true;
                }
            } else {
                return false;
            }
        }
        return false;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读