下面是我自己的编写的代码,感觉还能再优化。
希望有大神可以分享一下自己的解决方案
1 let arr = [3,2,1,14,5,5,8,1,2,3,4,5,6,76,7,1,2,9]; 2 3 function fn(arr){ 4 let temp = []; 5 let 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{ 10 if(temp.length!=0){ 11 let temp1 = []; 12 temp.push(arr[i]); 13 14 for( let i = 0 ; i < temp.length; i++){ 15 temp1.push(temp[i]) 16 } 17 18 if(sub.length===0||sub.length<temp1.length){ 19 sub = temp1 20 } 21 temp = []; 22 } 23 } 24 } 25 return sub; 26 } 27 let arr1 = fn(arr); 28 console.log(arr1);
原文地址:https://www.cnblogs.com/ligusuni/p/9571023.html
时间: 2024-10-10 08:56:32