MCPcopy
hub / github.com/qiyuangong/leetcode / longestConsecutive

Method longestConsecutive

python/128_Longest_Consecutive_Sequence.py:2–27  ·  view source on GitHub ↗

:type nums: List[int] :rtype: int

(self, nums)

Source from the content-addressed store, hash-verified

1class Solution(object):
2 def longestConsecutive(self, nums):
3 """
4 :type nums: List[int]
5 :rtype: int
6 """
7
8 def longestConsecutive(self, num):
9 # Pop adjacency O(n) and O(n)
10 num = set(num)
11 maxLen = 0
12 while num:
13 n = num.pop()
14 i = n + 1
15 l1 = 0
16 l2 = 0
17 while i in num:
18 num.remove(i)
19 i += 1
20 l1 += 1
21 i = n - 1
22 while i in num:
23 num.remove(i)
24 i -= 1
25 l2 += 1
26 maxLen = max(maxLen, l1 + l2 + 1)
27 return maxLen

Callers

nothing calls this directly

Calls 2

popMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected