-
Notifications
You must be signed in to change notification settings - Fork 43
/
contains-duplicate-ii.py
116 lines (100 loc) · 2.95 KB
/
contains-duplicate-ii.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
"""
# Time: O(n)
# Space: O(n)
#
# Given an array of integers and an integer k, return true if
# and only if there are two distinct indices i and j in the array
# such that nums[i] = nums[j] and the difference between i and j is at most k.
#
# V0
# V1
# https://blog.csdn.net/coder_orz/article/details/51674266
# IDEA : HASH TABLE
class Solution(object):
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
num_map = {}
for i in range(len(nums)):
if nums[i] in num_map and i - num_map[nums[i]] <= k:
return True
else:
num_map[nums[i]] = i
return False
# V1'
# https://blog.csdn.net/coder_orz/article/details/51674266
# IDEA : SET
# IDEA : SET OPERATION
# In [12]: window = set([1,3,4,])
# ...:
# In [13]: window
# Out[13]: {1, 3, 4}
# In [14]: window.discard(1)
# In [15]: window
# Out[15]: {3, 4}
# In [16]: window.discard(3)
# In [17]: window
# Out[17]: {4}
class Solution(object):
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
window = set([])
for i in range(len(nums)):
if i > k:
window.discard(nums[i-k-1])
if nums[i] in window:
return True
else:
window.add(nums[i])
return False
# V1'
# https://www.jiuzhang.com/solution/contains-duplicate-ii/#tag-highlight-lang-python
class Solution:
"""
@param nums: the given array
@param k: the given number
@return: whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k
"""
def containsNearbyDuplicate(self, nums, k):
# Write your code here
dic = {}
for index, value in enumerate(nums):
if value in dic and index - dic[value] <= k:
return True
dic[value] = index
return False
# V2
class Solution:
# @param {integer[]} nums
# @param {integer} k
# @return {boolean}
def containsNearbyDuplicate(self, nums, k):
lookup = {}
for i, num in enumerate(nums):
if num not in lookup:
lookup[num] = i
else:
# It the value occurs before, check the difference.
if i - lookup[num] <= k:
return True
# Update the index of the value.
lookup[num] = i
return False