码迷,mamicode.com
首页 > 其他好文 > 详细

Jump Game

时间:2017-05-25 10:07:05      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:log   jump game   keep   com   list   distance   public   ast   logs   

Note:
From back to start, if A[distance] is larger than the distance to the last element, it indicates that the last element is reachable. We can move the last element to the previous one. Otherwise, keep adding the distance and compare A[distance] till distance to the first element. 

public class Solution {
    /**
     * @param A: A list of integers
     * @return: The boolean answer
     */
    public boolean canJump(int[] A) {
        // wirte your code here
        if (A == null || A.length == 0) {
            return false;
        }
        
        if (A[0] == 0 && A.length == 1) {
            return true;
        }
        
        int n = A.length;
        int dis = 1;
        int i = n - 1;
        
        while (i >= 1 && i >= dis) {
            if (A[i - dis] >= dis) {
                dis = 1;
                i--;
            } else {
                dis++;
            }
        }
        
        if (i == 0 && A[i] != 0) {
            return true;
        }
        return false;
    }
}

 

Jump Game

标签:log   jump game   keep   com   list   distance   public   ast   logs   

原文地址:http://www.cnblogs.com/codingEskimo/p/6901939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!