LintCode-跳跃游戏

给出一个非负整数数组,你最初定位在数组的第一个位置。   

数组中的每个元素代表你在那个位置可以跳跃的最大长度。    

判断你是否能到达数组的最后一个位置。

样例

A = [2,3,1,1,4],返回 true.

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

分析:遍历一遍,记录可走的点可以达到的最远点

代码:

class Solution {
public:
    /**
     * @param A: A list of integers
     * @return: The boolean answer
     */
    bool canJump(vector<int> A) {
        // write you code here
        int maxN = 0;
        int i = 0;
        while(i<=maxN)
        {
            maxN = max(maxN,i+A[i]);
            i++;
            if(maxN>=A.size()-1)
                return true;
        }
        return false;
    }
};

    原文作者:LintCode题目解答
    原文地址: https://blog.csdn.net/wangyuquanliuli/article/details/45896269
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞