128

mac2026-03-20  3

""" 给定一个未排序的整数数组,找出最长连续序列的长度。 要求算法的时间复杂度为 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) # get方法获取键对应的值 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))

最新回复(0)