var longestConsecutive = function(nums) {
if(nums.length===0)return 0;
//去重
nums = Array.from(new Set(nums));
//排序
nums.sort((x,y)=>x-y);
let ans = 1;
let idx = 0;
for(let i=1;i<nums.length;i++){
if(nums[i]-nums[i-1]!==1){
ans = Math.max(ans, i-idx);
idx = i;
}
}
ans = Math.max(ans, nums.length-idx);
return ans;
};
128
- 本文链接:http://gulinga.github.io/2022/09/21/arithmetic/LeetCode%E7%83%AD%E9%A2%98HOT100/128/
- 版权声明:本博客所有文章除特别声明外,均默认采用 许可协议。