My simple C++ solution (3 lines)

  • 0

    Nothing special, just always try to extend the range the jump can reach. In the below implementation, rightEnd is used to record the rightmost end the jump can reach. If it is no less than len-1, then it can reach the last element so return true. Otherwise, move towards the right side in the reachable range (i.e. for loop, and i<=rightEnd) trying to extend the reachable range (i.e. increase rightEnd).

    class Solution {
        bool canJump(vector<int>& nums) {
            int len = nums.size(), rightEnd = 0;
            for(auto i=0; i<=rightEnd && rightEnd < (len-1); ++i) rightEnd = max(rightEnd , i + nums[i]) ;
            return rightEnd>=(len-1);

Log in to reply

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