【LeetCode】55. Jump Game

Jump Game

 

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.

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

A = [3,2,1,0,4], return false.

 

不断更新能达到的最远范围rch,

如果rch >= n-1,说明到达最后,返回true

否则返回false

class Solution {
public:
    bool canJump(int A[], int n) {
        int rch = 0;
        for(int i = 0; i <= rch; i ++)
        {
            rch = max(rch, A[i]+i);
            if(rch >= n-1)
                return true;
        }
        return false;
    }
};

 

posted @ 2014-05-04 20:48  陆草纯  阅读(511)  评论(0编辑  收藏  举报