:type nums: List[int] :rtype: int
(self, nums)
| 1 | class 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 |