55.jump game


贪心算法:如果绕不过某个零的话 就会return false 如果能绕过去 就一定能达到对面

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(nums[i]+i,max);
    }
        return true;
    }
    }

动态规划:

class Solution {
    public boolean canJump(int[] nums) {
        int length = nums.length;
        int lastCan = length-1;
        for(int i=length-1;i>=0;i--) {
            if(nums[i]+i >= lastCan) {
                lastCan = i;
                if(nums[0] >= lastCan)
                    return true;
            }
        }
        return false;
    }
}
原文地址:https://www.cnblogs.com/cznczai/p/11150357.html