1 /*
 2  * @Author: yaodaoteng
 3  * @Date: 2020-12-02 16:44:04
 4  * @LastEditors: yaodaoteng
 5  * @LastEditTime: 2020-12-02 16:48:36
 6  * @FilePath: \leetcode\55.跳跃游戏.cpp
 7  */
 8 /*
 9  * @lc app=leetcode.cn id=55 lang=cpp
10  *
11  * [55] 跳跃游戏
12  */
13 
14 // @lc code=start
15 class Solution {
16 public:
17 /*
18 依次遍历数组中的每一个位置,并实时维护 最远可以到达的位置。对于当前遍历到的位置 x,如果它在 最远可以到达的位置 的范围内,那么我们就可以从起点通过若干次跳跃到达该位置,因此我们可以用 x+nums[x] 更新 最远可以到达的位置。
19 
20 在遍历的过程中,如果 最远可以到达的位置 大于等于数组中的最后一个位置,那就说明最后一个位置可达,我们就可以直接返回 True 作为答案。反之,如果在遍历结束后,最后一个位置仍然不可达,我们就返回 False 作为答案。
21 
22 */
23 
24     bool canJump(vector<int>& nums) {
25         int n = nums.size();
26         int rightmost = 0;
27         for (int i = 0; i < n;i++){
28             if(i<=rightmost){
29                 rightmost = max(rightmost,i + nums[i]);
30                 if(rightmost>=n-1)
31                     return true;
32             }
33         }
34         return false;
35     }
36 };
37 // @lc code=end