Skip to content

128. 最长连续序列 ​

  • 题号:128
  • 来源:LeetCode
  • 难度:中等
  • 标签:数组 哈希表 排序
  • 语言:Python
  • 解法:1 个
  • 作者:lmliheng
  • 最近更新:2026-09-29

Python · O(n log n) 时间 · O(1) 空间 · 更新于 2026-09-29

排序后扫描统计最长连续段

python
"""
@difficulty medium
@tags 数组,哈希表,排序
@time O(n log n)
@space O(1)
@note 排序后扫描统计最长连续段
最长连续序列
lc 128
"""
class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        if len(nums)==0:
            return 0

        nums.sort()
        print(nums)
        maxLen=1
        res=0
        for index,num in enumerate(nums):
            if index==0:
                continue
            if nums[index-1]==num-1:
                maxLen+=1
            elif nums[index-1]==num:
                continue
            else:
                res=max(res,maxLen)
                maxLen=1
        res=max(res,maxLen)
        return res

源码:python/leetcode/hot100/3.py


在 GitHub 上查看题目所在目录:lmliheng/algorithm