128. Longest Consecutive Sequence
比较简单,利用一下hashmap就可以了
class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
#用hashmap
h = {}
for n in nums:
h[n] = 1
length = 0
for key in h:
if h[key] == 1:
i = key + 1
j = key - 1
while i in h:
h[i] -= 1
i += 1
while j in h:
h[j] -= 1
j -= 1
length = max(i - 1 - j, length)
return length