首页 > 其他 > 详细

Jump Game

时间:2017-05-25 09:19:16      阅读:278      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!