-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContainDuplicates.py
More file actions
70 lines (59 loc) · 1.8 KB
/
ContainDuplicates.py
File metadata and controls
70 lines (59 loc) · 1.8 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
# 217. Contains Duplicates
# 219. Contains Duplicates II
# 220. Contains Duplicates III
class Solution:
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if not nums:
return False
dict = {}
for num in nums:
if not dict.get(num):
dict[num] = 1
else:
return True
return False
class Solution2:
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
if not nums:
return False
dict = {}
for i in range(len(nums)):
if not dict.get(nums[i]):
dict[nums[i]] = [i]
else:
if abs(i - dict[nums[i]][-1]) <= k:
return True
else:
dict[nums[i]].append(i)
return False
class Solution3:
# 维持一个k长度的窗口,遍历到第i项时,在窗口中寻找是否存在数num,使得nums[i] - t <= num <= nums[i] + t。
# 正常遍历窗口k,需要时间复杂度为O(nk),使用基于BST的结构(TreeSet,Python中没有),可以在logk时间内找出是否存在。
def containsNearbyAlmostDuplicate(self, nums, k, t):
"""
:type nums: List[int]
:type k: int
:type t: int
:rtype: bool
"""
if not nums or k == 0 or k >= 10000:
return False
s = set()
s.add(nums[0])
for i in range(1, len(nums)):
for elem in s:
if abs(elem - nums[i]) <= t:
return True
s.add(nums[i])
if len(s) == k + 1:
s.remove(nums[i - k])
return False