My AC Java Solution


  • 0
    S

    scan from right to left.
    set the last index to end and find another index i before end can reach end and then send end to i. do it until i = 0;

    public class Solution {
        public boolean canJump(int[] nums) {
            int n=nums.length;
            if(n<2) return true;
            int end=n-1;
            int i=n-2;
            boolean canr=false;
            while(i>=0){
                if(nums[i]>=end-i){  //find an i that nums[i]+i can reach the current end;
                    end=i;  //update the end to i;
                    i--;
                    canr=true;
                } else { //cannot reach the end
                    i--; //continue to try;
                    canr=false;
                }
            }
            return canr;
        }
    }
    '''
    

Log in to reply
 

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