-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathContainsDuplicateIII_v0.py
More file actions
41 lines (33 loc) · 1.12 KB
/
ContainsDuplicateIII_v0.py
File metadata and controls
41 lines (33 loc) · 1.12 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
#!/usr/bin/env python
# encoding: utf-8
class Solution:
# @param {integer[]} nums
# @param {integer} k
# @param {integer} t
# @return {boolean}
def containsNearbyAlmostDuplicate(self, nums, k, t):
for i in range(len(nums)):
_nums = nums[i+1:i+k+1]
n = nums[i]
for j in range(len(_nums)):
m = _nums[j]
if abs(m - n) <= t:
return True
return False
def containsNearbyAlmostDuplicate_E(self, nums, k, t):
m = {}
for i, n in enumerate(nums):
j = m.get(n)
for x in range(n-t, n+t+1):
m[x] = i
if j is not None:
if (i - j) <= k:
return True
return False
if __name__ == '__main__':
s = Solution()
print s.containsNearbyAlmostDuplicate([1,5,8,9,16], 1, 1)
print s.containsNearbyAlmostDuplicate([1,5,8,16], 1, 3)
print s.containsNearbyAlmostDuplicate([0,2147483647], 1, 2147483647)
print s.containsNearbyAlmostDuplicate([-3,3], 2, 4)
print s.containsNearbyAlmostDuplicate([4,2], 2, 1)