[leetcode]jump game

class Solution {
public:
    bool canJump(int A[], int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(n == 0) return false;
        if(n == 1) return true;
        
        int range = A[0];
        for(int i = 1; i < n; i++){
            if(range >= n-1) return true;
            if(i <= range){
                range = max(range, A[i] + i);
            }
        }
        return false;
        
    }
};

你可能感兴趣的:([leetcode]jump game)