题意为找到最长的数字连续序列的长度
思路:首先使用set()保证元素唯一,然后找到数字的起点,下面就是每次增一找到终点,不断更新最后最大长度
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
ans = 0
st = set(nums)
for x in st:
if x - 1 in st:
continue
y = x + 1
while y in st:
y += 1
ans = max(ans, y - x)
return ans