Skip to content

Commit a4268c0

Browse files
Solve : Longest Consecutive Sequence
1 parent e0d479f commit a4268c0

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def longestConsecutive(self, nums):
3+
num_set = set(nums)
4+
longest = 0
5+
for num in num_set:
6+
if num - 1 not in num_set:
7+
current = num
8+
length = 1
9+
while current + 1 in num_set:
10+
current += 1
11+
length += 1
12+
longest = max(longest, length)
13+
return longest

0 commit comments

Comments
 (0)