跳跃游戏 (Jump Game)

 

思路:

// @Title: 跳跃游戏 (Jump Game)
// @Author: qisiii
// @Date: 2024-05-10 18:19:27
// @Runtime: 2 ms
// @Memory: 44.5 MB
// @comment: 
// @flag: 
class Solution {
    public boolean canJump(int[] nums) {

        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            if (max < i) {
                return false;
            }
            max = Math.max(max, i + nums[i]);
        }
        return true;
    }
}

+++ title = “跳跃游戏 (Jump Game)” draft = false +++

思路:

// @Title: 跳跃游戏 (Jump Game)
// @Author: qisiii
// @Date: 2024-09-22 17:52:50
// @Runtime: 2 ms
// @Memory: 44.6 MB
// @comment: 
// @flag: 
class Solution {
    public boolean canJump(int[] nums) {
        if(nums.length<=1){
            return true;
        }
        int target=0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]==0&&target<=i&&i!=nums.length-1){
                return false;
            }
            target=Math.max(target,i+nums[i]);
        }
        return true;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18