We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ef38f39 commit 2d4f7c2Copy full SHA for 2d4f7c2
longest-consecutive-sequence/ppxyn1.py
@@ -1,9 +1,24 @@
1
-# idea: -
2
-
+# idea: Hash
3
class Solution:
4
def longestConsecutive(self, nums: List[int]) -> int:
5
- ans = 0
6
- return ans
+ if not nums:
+ return 0
+
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
22
23
24
@@ -33,3 +48,4 @@ def longestConsecutive(self, nums: List[int]) -> int:
33
48
34
49
35
50
51
0 commit comments