# 解题思路:用查找表(集合),保存其K个值的状态 遍历查找表时加上了限制条件T 20190302 找工作期间
class Solution(object):
def containsNearbyAlmostDuplicate(self, nums, k, t):
"""
:type nums: List[int]
:type k: int
:type t: int
:rtype: bool
"""
if len(nums) <= 1:
return False
if k<=0 or t < 0:
return False
record = set()
l = len(nums)
for i in range(l):
if t == 0:
if nums[i] in record:
return True
else:
for j in record:
if abs(nums[i]-j) <= t:
return True
record.add(nums[i])
if len(record) == k+1:
record.remove(nums[i-k])
return False
class Solution2(object):
def containsNearbyAlmostDuplicate(self, nums, k, t):
"""
:type nums: List[int]
:type k: int
:type t: int
:rtype: bool
"""
lenth = len(nums)
a = set()
for i in range(lenth):
if t==0:
if nums[i] in a:
return True
else:
for atem in a:
if abs(nums[i]-atem)<=t:
return True
a.add(nums[i])
if len(a) == k+1:
a.remove(nums[i-k])
return False