-
Notifications
You must be signed in to change notification settings - Fork 0
/
128.最长连续序列.py
63 lines (61 loc) · 1.28 KB
/
128.最长连续序列.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
# @lc app=leetcode.cn id=128 lang=python3
#
# [128] 最长连续序列
#
# https://leetcode.cn/problems/longest-consecutive-sequence/description/
#
# algorithms
# Medium (55.13%)
# Likes: 1431
# Dislikes: 0
# Total Accepted: 326.1K
# Total Submissions: 591.4K
# Testcase Example: '[100,4,200,1,3,2]'
#
# 给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。
#
# 请你设计并实现时间复杂度为 O(n) 的算法解决此问题。
#
#
#
# 示例 1:
#
#
# 输入:nums = [100,4,200,1,3,2]
# 输出:4
# 解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
#
# 示例 2:
#
#
# 输入:nums = [0,3,7,2,5,8,4,6,0,1]
# 输出:9
#
#
#
#
# 提示:
#
#
# 0
# -10^9
#
#
#
# @lc code=start
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
# 1. 哈希表 O(N) O(N)
max_len = 0
nums_set = set(nums)
for num in nums_set:
if num - 1 not in nums_set:
cur_num = num
cur_len = 1
while cur_num + 1 in nums_set:
cur_num += 1
cur_len += 1
max_len = max(max_len, cur_len)
return max_len
# @lc code=end