leetcode 55 跳跃游戏

class Solution {
public:
    bool canJump(vector<int>& nums) {
        //贪心算法:每次在可到达范围内的点,更新能到达的最远距离,如果最终这个距离大于len则说明可以到达终点,时间复杂度O(n);
        int len=nums.size();
        if(len==0) return true;
        int t=0;
        for(int i=0;i<=t&&i<len;i++){
            t=max(t,i+nums[i]);
        }
        return t>=len-1;
    }
};

猜你喜欢

转载自www.cnblogs.com/joelwang/p/10637661.html