(数组)最长的连续元素序列长度
/** * * @param num int整型一维数组 * @return int整型 */ function longestConsecutive( num ) { // write code here var set = new Set(num); var max = 1; for(var i=0;i<num.length;i++){ if(set.delete(num[i])){ var large_val = num[i]+1; var small_val = num[i]-1; var count = 1; while(set.delete(large_val)){ count++; large_val++; } while(set.delete(small_val)){ count++; small_val--; } max = Math.max(max,count) } } return max; } module.exports = { longestConsecutive : longestConsecutive };
其他算法 文章被收录于专栏
其他算法