美文网首页
55、Jump Game

55、Jump Game

作者: 小鲜贝 | 来源:发表于2018-04-18 19:18 被阅读0次

Example
非负整数数组,可以初始化第一次的位置. 每个元素的值代表跳跃的最长位置
返回是否可以跳完数组

For example: 
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.

思路
不断更新终点位置,直到超出数组

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

相关文章

网友评论

      本文标题:55、Jump Game

      本文链接:https://www.haomeiwen.com/subject/nqsbkftx.html