var arr = [11,10,9,2,5,3,7,101,18,19,20,21] ;
arr.sort(sortNumber);
console.log(arr);
var max = 0;
var temp = 0;
for(i=0;i<arr.length;i++){
if (i>0){
if(arr[i-1]+1==arr[i]){
max = max +1;
}else{
max = 0;
}
temp = temp>max?temp:max;
}
}
temp += 1;
console.log('最长自增子数组:'+temp);
function sortNumber(a,b){
return a - b
}
先排序,再循环计算最长自增