My C++ solution with explanation, which defeat 60%


  • 0

    (1) if you can arrive the ith location, you can arrivethe jth location(j < i).

    (2)so that add the largest length you can arrive.

    note that: limit is the largest length you can arrive; index is the current location.

    class Solution {
    public:
        bool canJump(vector<int>& nums) {
            int limit = nums[0], index = 0;
            while(index < nums.size() && index < limit)
            {
                index++;
                if(nums[index] + index > limit)
                {
                    limit = nums[index] + index;
                }
            }
            if(index >= nums.size() - 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.