Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.

Example 1:

Input: nums = [1,2,3,1], k = 3, t = 0
Output: true

Example 2:

Input: nums = [1,0,1,1], k = 1, t = 2
Output: true

Example 3:

Input: nums = [1,5,9,1,5,9], k = 2, t = 3
Output: false

给定一个数组和两个整数t 和k ,求是否有不同的两个下标i和j,满足|nums[i] – nums[j]|<= t && | i – j | <=k

解法:参考: 细语呢喃

Java:

class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (k <= 0 || t < 0) return false;
TreeSet<Long> tree = new TreeSet<>();
for (int i = 0; i < nums.length; i++) {
Long x = tree.ceiling((long) nums[i] - t);
if (x != null && x <= (long) nums[i] + t) return true;
if (i >= k)
tree.remove((long) nums[i - k]);
tree.add((long) nums[i]);
}
return false;
}
}  

Python:

class Solution:
# @param {integer[]} nums
# @param {integer} k
# @param {integer} t
# @return {boolean}
def containsNearbyAlmostDuplicate(self, nums, k, t):
if k < 0 or t < 0:
return False
window = collections.OrderedDict()
for n in nums:
# Make sure window size
if len(window) > k:
window.popitem(False) bucket = n if not t else n // t
# At most 2t items.
for m in (window.get(bucket - 1), window.get(bucket), window.get(bucket + 1)):
if m is not None and abs(n - m) <= t:
return True
window[bucket] = n
return False 

C++:

class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
map<long long, int> m;
int j = 0;
for (int i = 0; i < nums.size(); ++i) {
if (i - j > k) m.erase(nums[j++]);
auto a = m.lower_bound((long long)nums[i] - t);
if (a != m.end() && abs(a->first - nums[i]) <= t) return true;
m[nums[i]] = i;
}
return false;
}
};

  

类似题目:

[LeetCode] 217. Contains Duplicate 包含重复元素

[LeetCode] 219. Contains Duplicate II 包含重复元素 II

  

All LeetCode Questions List 题目汇总

[LeetCode] 220. Contains Duplicate III 包含重复元素 III的更多相关文章

  1. [LeetCode] 219. Contains Duplicate II 包含重复元素 II

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  2. LeetCode 217. Contains Duplicate (包含重复项)

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  3. [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)

    每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...

  4. [LeetCode] 217. Contains Duplicate 包含重复元素

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  5. [LeetCode] Contains Duplicate III 包含重复值之三

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  6. Java实现 LeetCode 220 存在重复元素 III(三)

    220. 存在重复元素 III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最 ...

  7. Leetcode 220.存在重复元素III

    存在重复元素III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ. ...

  8. 【每日算法】存在重复元素 III

    题目描述 这是 LeetCode 上的 220. 存在重复元素 III, 难度为 [中等] 给你一个整数数组 nums 和两个整数 k 和 t .请你判断是否存在 两个不同下标 i 和 j,使得 ab ...

  9. 从n个元素中选择k个的所有组合(包含重复元素)

    LeetCode:Combinations这篇博客中给出了不包含重复元素求组合的5种解法.我们在这些解法的基础上修改以支持包含重复元素的情况.对于这种情况,首先肯定要对数组排序,以下不再强调 修改算法 ...

随机推荐

  1. 如何为SUSE配置IP地址,网关和DNS

    方法一.在命令行中配置.输入: ifconfig eht0 9.111.66.96 netmask 255.255.255.0 up route add default gw 9.111.66.1 方 ...

  2. Codeforces B. Mouse Hunt(强连通分解缩点)

    题目描述: Mouse Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. Kotlin调用Java程序重点分析

    在上一次https://www.cnblogs.com/webor2006/p/11530801.html中学习了Kotlin调用Java的使用方式及一些注意点,这次继续其这个场景进一步学习. 数组( ...

  4. SQLAlchemy多对多

    创建多对多表 from sqlalchemy.ext.declarative import declarative_base Base=declarative_base() from sqlalche ...

  5. Python 简单批量请求接口实例

    #coding:utf-8 ''' Created on 2017年11月10日 @author: li.liu ''' import urllib import time str1=''' http ...

  6. js判断是否第一次访问跳转

    今天分享一套关于Js劫持代码,进行判断第一次访问进行跳转,仅供大家参考学习! 未加密: if (c.indexOf('isfirstvisited=false') != -1) { } else { ...

  7. Laravel —— tips 总结

    一.Laravel 中 ajax 请求需要设置 header $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token& ...

  8. 启用Microsoft loopback Adapte

    开始▶控制面板▶系统   系统▶设备管理器   此时,点击操作的菜单是没有有用子菜单的,需要点击一下网络适配器.   再点击操作▶添加过时硬件   添加硬件向导▶下一步   安装我手动从列表选择的硬件 ...

  9. Vigil 发送多人邮件通知的处理

    Vigil 默认是只能发送单人邮件,但是我们有需要发送多个的场景. 解决方法: 大家使用一样的账户登陆 使用邮件组 修改下源码 为了学习下Vigil 的构建,以及原理,我简单通过修改源码的方式(目前支 ...

  10. CF #365 DIV2 D Mishka and Interesting sum 区间异或+线段树

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...