Skip to content

Commit 2d4f7c2

Browse files
committed
[:fixed] logest-consecutive-sequence
1 parent ef38f39 commit 2d4f7c2

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

longest-consecutive-sequence/ppxyn1.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1-
# idea: -
2-
1+
# idea: Hash
32
class Solution:
43
def longestConsecutive(self, nums: List[int]) -> int:
5-
ans = 0
6-
return ans
4+
if not nums:
5+
return 0
6+
7+
num_set = set(nums)
8+
max_len = 1
9+
10+
for num in num_set:
11+
if num - 1 not in num_set:
12+
current = num
13+
tmp = 1
14+
while current + 1 in num_set:
15+
current += 1
16+
tmp += 1
17+
if tmp > max_len:
18+
max_len = tmp
19+
return max_len
20+
21+
722

823

924

@@ -33,3 +48,4 @@ def longestConsecutive(self, nums: List[int]) -> int:
3348

3449

3550

51+

0 commit comments

Comments
 (0)