"""
给定一个未排序的整数数组,找出最长连续序列的长度。
要求算法的时间复杂度为 O(n)。
示例:
输入: [100, 4, 200, 1, 3, 2]
输出: 4
解释: 最长连续序列是 [1, 2, 3, 4]。它的长度为 4。
"""
def longestConsecutive(nums
):
hash_dict
= dict()
max_length
= 0
for num
in nums
:
if num
not in hash_dict
:
left
= hash_dict
.get
(num
-1, 0)
right
= hash_dict
.get
(num
+1, 0)
cur_length
= 1 + left
+ right
if cur_length
> max_length
:
max_length
= cur_length
hash_dict
[num
] = cur_length
hash_dict
[num
-left
] = cur_length
hash_dict
[num
+right
] = cur_length
return max_length
nums
= [100, 4, 200, 1, 3, 2]
print(longestConsecutive
(nums
))
转载请注明原文地址: https://mac.8miu.com/read-512499.html