const longestConsecutive = (nums = [0,1,2,4,8,5,6,7,9,3,55,88,77,99,999999999]) => { const length = nums.length if(length === 0 || length === 1) return length nums = nums.sort((x, y) => x - y) let len = 1 let startIdx = 1 for(let i = 1; i < nums.length; i++){ const pre = nums[i - 1] const cur = nums[i] if(pre === cur){ continue }else if(cur - pre === 1){ startIdx++ if(startIdx > len){ len = startIdx } }else{ startIdx = 1 } } return len };
Leecode提交通过
标签:pre,startIdx,const,nums,len,length,连续,序列,最长 From: https://www.cnblogs.com/zhenjianyu/p/17082227.html