图示

代码
python
# encoding = utf-8
# 开发者:Alen
# 开发时间: 12:31
# "Stay hungry,stay foolish."
class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
Setnum = set(nums)
longest = 0
for num in Setnum:
if (num - 1) not in Setnum:
longe = 1
while (num + longe) in Setnum:
longe += 1
longest = max(longest, longe)
return longest
结果
解题步骤:https://www.bilibili.com/video/BV14Z9TBNEJk/?vd_source=15b4bc8968fa5203cc470cb68ff72c96
