In an unordered array of integers, find the longest segment of a continuous growth fragment with a growth step of 1. Example: [3,2,4,5,6,1,9], the longest is [4,5,6]

Source: Internet
Author: User

In an unordered array of integers, find the longest segment of a continuous growth fragment with a growth step of 1. Example: [3,2,4,5,6,1,9], the longest is [4,5,6]

Here is my own code, and I feel I can optimize it.

I hope a great God can share his own solution.

1Let arr = [3,2,1,14,5,5,8,1,2,3,4,5,6,76,7,1,2,9];2 3 functionfn (arr) {4Let temp = [];5Let sub = [];6      for(Let i = 0; i < arr.length; i++ ){7         if(arr[i]+1 = = = Arr[i+1]){8 Temp.push (Arr[i]);9}Else{Ten             if(temp.length!=0){ OneLet Temp1 = []; A Temp.push (Arr[i]); -                  -                  for(Let i = 0; i < temp.length; i++){ the Temp1.push (Temp[i]) -                 } -                  -                 if(sub.length===0| | sub.length<temp1.length) { +Sub =Temp1 -                 } +temp = []; A             } at         } -     } -     returnSub; - } -Let arr1 =fn (arr); -Console.log (ARR1);

In an unordered array of integers, find the longest segment of a continuous growth fragment with a growth step of 1. Example: [3,2,4,5,6,1,9], the longest is [4,5,6]

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.