-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongest_Consecutive_Sequence.py
48 lines (35 loc) · 1.56 KB
/
Longest_Consecutive_Sequence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 思路一的py代码
# Runtime: 40 ms, faster than 88.35% of Python3 online submissions for Longest Consecutive Sequence.
# Memory Usage: 12.9 MB, less than 99.52% of Python3 online submissions for Longest Consecutive Sequence.
class Solution:
def longestConsecutive(self, nums: 'List[int]') -> 'int':
if not nums:
return 0
nums.sort()
Length, maxLength = 1, 1
for i in range(1, len(nums)):
if nums[i] == nums[i - 1] + 1:
Length += 1
maxLength = max(maxLength, Length)
elif nums[i] == nums[i - 1]:
pass
else:
Length = 1
return maxLength
# 思路二的py代码
# Runtime: 40 ms, faster than 88.35% of Python3 online submissions for Longest Consecutive Sequence.
# Memory Usage: 13.4 MB, less than 66.67% of Python3 online submissions for Longest Consecutive Sequence.
class Solution:
def longestConsecutive(self, nums: 'List[int]') -> 'int':
hashmap = dict()
maxLength = 0
for num in nums:
if num not in hashmap:
right = hashmap[num + 1] if num + 1 in hashmap else 0
left = hashmap[num - 1] if num - 1 in hashmap else 0
length = left + right + 1
maxLength = max(length, maxLength)
hashmap[num] = length
hashmap[num - left] = length
hashmap[num + right] = length
return maxLength