LeetCode 55(Jump Game) Java

原题:Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

给定一个数组,数组每位代表可走的步数,看能否到达数组尾部;

思路:判断法与Jump Game II 一致,用一个变量记录步数,当前到不了说明要多走一步;

本问题在于判断什么时候到不了数组尾部;

当某元素是0,且必须要经过这个元素,且该元素不在数组尾,此时该数组无法走到尾部;


代码:

public class Solution {
    public boolean canJump(int[] nums) {
       int maxRch=0;
       int maxVal=0;
       for(int i=0;i(i+nums[i])?maxVal:(i+nums[i]);
       }
       return maxRch>=nums.length-1;
    }
}


你可能感兴趣的:(LeetCode)