Jump Game | Java最短代码实现

原题链接:55. Jump Game

【思路】

本题扩展:Jump Game 。对任意一点i,maxIndex记录当前能跳到的最大索引,当这个值大于等于nums.length - 1,则返回true,否则返回false。当然nums.length = 1时也返回true:

    public boolean canJump(int[] nums) {
        int maxIndex = nums[0];
        for (int i = 1; i <= maxIndex; i++) {
            if (maxIndex >= nums.length - 1) return true;
            maxIndex = Math.max(maxIndex, nums[i] + i);
        }
        return nums.length == 1 ? true : false;
    }

72 / 72 test cases passed. Runtime: 3 ms  Your runtime beats 27.06% of javasubmissions.

欢迎优化!

你可能感兴趣的:(LeetCode)