-
Notifications
You must be signed in to change notification settings - Fork 0
/
contains_dupes.py
65 lines (46 loc) · 1.08 KB
/
contains_dupes.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
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if len(nums) <= 1:
return False
_map = {}
for n in nums:
if _map.get(n):
return True
else:
_map[n] = True
return False
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
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.
"""
'''
This times out on long Ks:
if len(nums) <= 1:
return False
for i, n in enumerate(nums):
for j in range(i + 1, i + k + 1):
try:
if n == nums[j]:
return True
except:
pass
return False
'''
if len(nums) <= 1:
return False
for i, n in enumerate(nums):
assert Solution().containsNearbyDuplicate([1,2,3,1], 3) == True
assert Solution().containsNearbyDuplicate([1,0,1,1], 1) == True
assert Solution().containsNearbyDuplicate([1,2,3,1,2,3], 2) == False