LeetCode OJ-- Jump Game

https://oj.leetcode.com/problems/jump-game/

从0开始,根据每一位上存的数值往前跳。

这道题给想复杂了。。。

记录当前位置 pos,记录可以调到的最远达位置为far,并随时更新它

如果 far >= n - 1 则相当于调到末尾了,在此过程中 pos <= far

class Solution {
public:
    bool canJump(int A[], int n) {
        if(n==0)
            return false;

        if(n==1)
            return true;

        int far = A[0] + 0;
        int current = 0;
        while(current<=far &&current<n)
        {
            if(far >= n - 1)
                return true;
            if(far<A[current] + current)
                far = A[current] + current;
            current++;
        }

        return false;
    }
};
原文地址:https://www.cnblogs.com/qingcheng/p/3827035.html