有点最小滑动窗口的思想,用有序字典来维护这个窗口
class Solution:
def containsNearbyAlmostDuplicate(self, nums, k, t):
"""
:type nums: List[int]
:type k: int
:type t: int
:rtype: bool
"""
import collections
dic = collections.OrderedDict()
if k<1 or t<0: return False
for n in nums:
key=n//t if t else n
for m in [dic.get(key-1),dic.get(key),dic.get(key+1)]:
if m!=None and abs(n-m)<=t:
return True
if len(dic)==k:
dic.popitem(last=False)
dic[key]=n
return False