Leetcode Jump Game-----java

Source: Internet
Author: User

Given an array of non-negative integers, you is initially positioned at the first index of the array.

Each element of the array represents your maximum jump length is at that position.

Determine if you is able to reach the last index.

For example:
A = [2,3,1,1,4] , return true .

A = [3,2,1,0,4] , return false .

The problem is relatively simple compared to JUMP2, just ask if it can all reach the end

The longest distance maxlen is recorded for the first time, if the farthest distance is the same as the current position. Then you cannot reach the end, if the farthest distance exceeds the length-1, then the direct return true, although AC, but the result is not very good.

 Public classSolution { Public BooleanCanjump (int[] nums) {        intMaxLen = Nums[0]; intLen =nums.length; if(Len < 2 )            return true; if(MaxLen = = 0)            return false;  for(inti = 1;i<len;i++) {MaxLen= (I+nums[i]) >maxlen? (i+Nums[i]): MaxLen; if(i = =maxlen)return false; if(Len-1 <=maxlen)return true; }        returnLen<=maxlen-1?true:false; }}

Then slightly modified the code, the speed has improved. (It is also very strange that the same code, the runtime may be inconsistent each time the commit.) )

 Public classSolution { Public BooleanCanjump (int[] nums) {        intMaxLen = Nums[0]; intLen = Nums.length; if(Len < 2 )            return true; if(MaxLen = = 0)            return false; if(MaxLen >= len-1)            return true;  for(inti = 1;i<len;i++) {Nowlen= i+Nums[i]; if(Nowlen >maxlen) {MaxLen=Nowlen; if(Len-1 <=maxlen)return true; }            Else if(i = =maxlen)return false; }        return true; }}

After looking at the official solution, it was found that there was a simpler way of writing, from the back to the convenience. Although time complexity and spatial complexity have not changed, but the code is a lot less, but it is strange that even the official answer, occasionally will not AC, there is a time-out, there is the two answers are the same.

 Public class Solution {    publicboolean canjump (int[] nums) {        int Lastpos = Nums.length-1;          for (int i = nums.length-1; I >= 0; i--) {            if (i + nums[i] >= lastpos) {
    = i;            }        }         return Lastpos = = 0;    }}

Leetcode Jump Game-----java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.