-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.py
More file actions
37 lines (27 loc) · 782 Bytes
/
Copy pathSolution.py
File metadata and controls
37 lines (27 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from _ast import List
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
if nums is None:
return 0
if len(nums) <= 1:
return len(nums)
map = {}
for num in nums:
map[num] = 1
ans = 0
for num in nums:
if num in map:
length = 1
left = num - 1
right = num + 1
while left in map:
del map[left]
length += 1
left -= 1
while right in map:
del map[right]
length += 1
right += 1
if ans < length:
ans = length
return ans