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
}
leetcode 原题 https://leetcode.com/problems...
我的代码 https://github.com/hanzichi/l...
我的题解 https://github.com/hanzichi/l...
没看懂题。
【2,5】【3,7】【3,7,101】【7,101】?
还是
[2,3,7,101]?
http://blog.csdn.net/wangxin1...
这个应该是你想要的吧
先排序,再循环计算最长自增