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]