class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
num_set = set(nums)
max_length = 0
for num in num_set:
# 只从序列的起点开始扩展
if num - 1 not in num_set:
current_num = num
current_length = 1
# 向右扩展,查找连续数字
while current_num + 1 in num_set:
current_num += 1
current_length += 1
# 更新最长长度
max_length = max(max_length, current_length)
return max_length
07-12
123
