Java solution without searching all elements


  • 0
    G
        public boolean canJump(int[] nums) {
            int l=nums.length;
            if (l<=1)
                return true;
            int n=0;
            while(nums[n]+n+1<l){
                n+=nums[n];
                if(nums[n]==0){
                    int k = n;
                    while(n+nums[n]<=k)
                        if(--n < 0)
                            return false;
                }
            }
            return true;
        }
    

    We have to do backward searching only if we jumped to 0.


Log in to reply
 

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