示例代码

class Solution {
public:
    bool canJump(vector<int>& nums) {
        int len=nums.size(),rightMost=0;
        for(int i=0;i<len;i++){
            //在rightMost覆盖的范围内,不断地扩大覆盖面积
            if(i<=rightMost){
                rightMost=max(rightMost,i+nums[i]);
                if(rightMost>=len-1){
                    return true;
                }
            }else{
                break;
            }
        }
        return false;
    }
};
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

效果展示

LeetCode---55. 跳跃游戏(贪心)_leetcode