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:

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

Example 2:

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

Example 3:

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

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

解法:参考: 细语呢喃

Java:

  1. class Solution {
  2. public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
  3. if (k <= 0 || t < 0) return false;
  4. TreeSet<Long> tree = new TreeSet<>();
  5. for (int i = 0; i < nums.length; i++) {
  6. Long x = tree.ceiling((long) nums[i] - t);
  7. if (x != null && x <= (long) nums[i] + t) return true;
  8. if (i >= k)
  9. tree.remove((long) nums[i - k]);
  10. tree.add((long) nums[i]);
  11. }
  12. return false;
  13. }
  14. }  

Python:

  1. class Solution:
  2. # @param {integer[]} nums
  3. # @param {integer} k
  4. # @param {integer} t
  5. # @return {boolean}
  6. def containsNearbyAlmostDuplicate(self, nums, k, t):
  7. if k < 0 or t < 0:
  8. return False
  9. window = collections.OrderedDict()
  10. for n in nums:
  11. # Make sure window size
  12. if len(window) > k:
  13. window.popitem(False)
  14.  
  15. bucket = n if not t else n // t
  16. # At most 2t items.
  17. for m in (window.get(bucket - 1), window.get(bucket), window.get(bucket + 1)):
  18. if m is not None and abs(n - m) <= t:
  19. return True
  20. window[bucket] = n
  21. return False 

C++:

  1. class Solution {
  2. public:
  3. bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
  4. map<long long, int> m;
  5. int j = 0;
  6. for (int i = 0; i < nums.size(); ++i) {
  7. if (i - j > k) m.erase(nums[j++]);
  8. auto a = m.lower_bound((long long)nums[i] - t);
  9. if (a != m.end() && abs(a->first - nums[i]) <= t) return true;
  10. m[nums[i]] = i;
  11. }
  12. return false;
  13. }
  14. };

  

类似题目:

[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. CentOS6.5_x64上简单编译配置Heartbeat3.0.4

    Heartbeat 3与 2.x的最大差别在于,3 按模块把的原来2.x 拆分为多个子项目,并且提供了一个cluster-glue的组件,专用于Local ResourceManager 的管理.即h ...

  2. 使用Arduino和SD卡模块记录数据并导出到Excel

    在本篇文章中,我们将学习如何基于Arduino开发板使用一个SD卡模块.同时结合DS3231实时时钟模块,我们将制作一个数据记录仪的示例,在示例中,我们将温度传感器的数据存储到SD卡中,并将这些数据导 ...

  3. Spring Cloud Zuul网关(快速搭建)

    zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架.相当于是设备和 Netflix ...

  4. python通过LXML库读取xml命名空间

    xml实例版本: <a> <city:table xmlns:city="city"> <heilongjiang name="citys& ...

  5. 6、Python基础语法

    一.Python输出 print是python输出的关键字,默认是输出内容后换行. 如果不想换行,需要在变量末尾加上 end="" . a = 'hello' b = 'world ...

  6. Jmeter扩展自定义函数

    步骤1.导入lib\ext下ApacheJMeter_core.jar和ApacheJMeter_functions.jar 步骤2.新建function的类的package声明必须已".f ...

  7. 《三体》刘慈欣英文演讲:说好的星辰大海你却只给了我Facebook

    美国当地时间2018日11月8日,著名科幻作家刘慈欣被授予2018年度克拉克想象力贡献社会奖(Clarke Award for Imagination in Service to Society),表 ...

  8. java 库存管理

     第一种方法: import java.util.Scanner; import java.util.Random; class kuCun { //库存管理 public static void m ...

  9. div+css制作哆啦A梦

    纯CSS代码加上 制作动画版哆啦A梦(机器猫) 哆啦A梦(机器猫)我们大家一定都很熟悉,今天给大家演示怎么用纯CSS.代码,来做一个动画版的哆啦A梦. 效果图: 下面代码同学可以查看一下,每个线条及椭 ...

  10. 第06组 Beta冲刺(1/4)

    队名:福大帮 组长博客链接: https://www.cnblogs.com/mhq-mhq/p/11990568.html 作业博客 : https://edu.cnblogs.com/campus ...