给定一个整数数组,判断其中是否存在两个不同的下标i和j满足:| nums[i] - nums[j] | <= t 并且 | i - j | <= k
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution: defcontainsNearbyAlmostDuplicate(self, nums, k, t): """ :type nums: List[int] :type k: int :type t: int :rtype: bool """ # 6 star, 有左右两个节点,右节点从第二个到最后一个,左节点则是右节点往左k个之间的范围 if t==0and len(nums)== len(set(nums)): returnFalse for i in range(len(nums)): for j in range(i+1, i+k+1): if j>=len(nums): break if abs(nums[i]-nums[j])<=t: returnTrue returnFalse