Skip to content

Commit 4ff4946

Browse files
committed
feat: add 0240 Longest Consecutive Sequence solution
1 parent 8e79dd9 commit 4ff4946

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def longestConsecutive(self, nums: List[int]) -> int:
3+
if not nums:
4+
return 0
5+
s = set(nums)
6+
nums = sorted(list(s))
7+
rs = []
8+
cnt = 1
9+
10+
for i in range(len(nums)-1):
11+
if (nums[i] + 1) == nums[i+1]:
12+
cnt += 1
13+
else:
14+
rs.append(cnt)
15+
cnt = 1
16+
17+
rs.append(cnt)
18+
return max(rs)
19+

0 commit comments

Comments
 (0)