# Longest Consecutive Sequence

### 描述​

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

Your algorithm should run in O(n) complexity.

### 代码​

# Longest Consecutive Sequence# Time Complexity: O(n)，Space Complexity: O(n)class Solution:    def longestConsecutive(self, nums: List[int]) -> int:        s = set(nums)        longest = 0        for num in s:            if num-1 not in s:                current_num = num                length = 1                while current_num+1 in s:                    current_num += 1                    length +=1                longest = max(longest, length)        return longest