LeetCode 55. 跳跃游戏

题目描述链接:https://leetcode-cn.com/problems/jump-game/

解题思路:贪心算法。从首节点开始,记录其能到达的最远距离。然后对于首节点能到达最远距离的节点中

,继续更新这些节点能够到达的最远的距离。如果存在达到最后节点的节点,则返回true。最后如果不能到达

返回false。最后LeetCode C++求解代码如下:

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

    }
};
原文地址:https://www.cnblogs.com/zzw-/p/13509917.html