forked from tcandzq/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestConsecutiveSequence.py
More file actions
73 lines (58 loc) · 2.01 KB
/
longestConsecutiveSequence.py
File metadata and controls
73 lines (58 loc) · 2.01 KB
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
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/8/30 15:31
# @Author : tc
# @File : longestConsecutiveSequence.py
"""
给定一个未排序的整数数组,找出最长连续序列的长度。
要求算法的时间复杂度为 O(n)。
Input1:[100, 4, 200, 1, 3, 2]
Output1:4
解释: 最长连续序列是 [1, 2, 3, 4]。它的长度为 4。
参考:https://leetcode-cn.com/problems/longest-consecutive-sequence/solution/zui-chang-lian-xu-xu-lie-by-leetcode/
这题的解法比较朴素,但优化的点很有意思:
1.查询某个数是否在数组当中可以用 HashSet 实现O(1)时间的查询;
2.我们只查找最长连续序列开头那个数字,这样就减少了作为序列一部分的数字的判断.
"""
#超时解答
def longestConsecutive(nums):
num_set = set(nums)
longest_streak = 0
for num in nums:
current_long = 1
current_num = num + 1
while current_num in num_set:
current_long += 1
current_num += 1
longest_streak = max(current_long, longest_streak)
return longest_streak
#参考解答:
def longestConsecutive2(nums):
num_set = set(nums)
longest_streak = 0
for num in nums:
current_long = 1
if num - 1 not in num_set:
current_num = num+1
while current_num in num_set:
current_long += 1
current_num += 1
longest_streak = max(current_long,longest_streak)
return longest_streak
#官方解答
def longestConsecutive3(nums):
longest_streak = 0
num_set = set(nums)
for num in num_set:
if num - 1 not in num_set:
current_num = num
current_streak = 1
while current_num + 1 in num_set:
current_num += 1
current_streak += 1
print(current_num)
longest_streak = max(longest_streak, current_streak)
return longest_streak
if __name__ == '__main__':
nums = [100, 4, 200, 1, 3, 2]
print(longestConsecutive3(nums))