55.Jump Game

class Solution {
public:
    bool canJump(vector& nums) {
        vector dp(nums.size(), 0);
        for (int i = 1; i < nums.size(); ++i) {
            dp[i] = max(dp[i - 1], nums[i - 1]) - 1;
            if (dp[i] < 0) return false;
        }
        return true;
    }
};
class Solution {
public:
    bool canJump(vector& nums) {
        int n = nums.size(), reach = 0;
        for (int i = 0; i < n; ++i) {
            if (i > reach || reach >= n - 1) break;
            reach = max(reach, i + nums[i]);
        }
        return reach >= n - 1;
    }
};

转载于:https://www.cnblogs.com/smallredness/p/10675305.html

你可能感兴趣的:(55.Jump Game)