Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d5312e7

Browse files
authored
Create 128-Longest-consecutive-sequence.py
1 parent dd7a02a commit d5312e7

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

128-Longest-consecutive-sequence.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def longestConsecutive(self, nums: List[int]) -> int:
3+
numSet = set(nums)
4+
longest = 0
5+
6+
for n in nums:
7+
# check if its the start of a sequence
8+
if (n - 1) not in numSet:
9+
length = 1
10+
while (n + length) in numSet:
11+
length += 1
12+
longest = max(length, longest)
13+
return longest
14+

0 commit comments

Comments
 (0)