Skip to content

Commit 0374317

Browse files
committed
longest-consecutive-sequence Solution
1 parent 360a77a commit 0374317

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def longestConsecutive(self, nums: List[int]) -> int:
3+
answer = []
4+
count = 0
5+
pre_num = -9999999999999999999
6+
for n in sorted(set(nums)):
7+
if pre_num + 1 == n:
8+
count += 1
9+
else:
10+
answer.append(count)
11+
count = 0
12+
pre_num = n
13+
14+
if count != 0:
15+
answer.append(count)
16+
17+
if answer:
18+
return max(answer) + 1
19+
else:
20+
return 0

0 commit comments

Comments
 (0)