输入:[10,9,2,5,3,7,101,18]
输出:4
根据输入数组的自增子数组,返回自增子数组的长度
没看懂题。
【2,5】【3,7】【3,7,101】【7,101】?
还是
[2,3,7,101]?
http://blog.csdn.net/wangxin1...
这个应该是你想要的吧
先排序,再循环计算最长自增
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
}
10 回答11.7k 阅读
2 回答3.2k 阅读✓ 已解决
4 回答2.2k 阅读✓ 已解决
3 回答1.2k 阅读✓ 已解决
3 回答839 阅读✓ 已解决
3 回答1k 阅读✓ 已解决
2 回答1.2k 阅读✓ 已解决
leetcode 原题 https://leetcode.com/problems...
我的代码 https://github.com/hanzichi/l...
我的题解 https://github.com/hanzichi/l...