Loading

【leetocode】55. Jump Game

  You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or false otherwise.

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

 

posted @ 2021-11-20 14:59  aalanwyr  阅读(15)  评论(0编辑  收藏  举报