leetcode题解(二十五):55. Jump Game

给定一个数组,判断你能不能跳到最后
例子:
Example 1:

Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:

Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
jump length is 0, which makes it impossible to reach the last index.

public boolean canJump(int[] A) {
    int max = 0;
    for(int i=0;i<A.length;i++){
        if(i>max) {return false;}
        //最多能跳到哪里
        max = Math.max(A[i]+i,max);
    }
    return true;
}

猜你喜欢

转载自blog.csdn.net/weixin_43869024/article/details/89430039