[LeetCode] 220. Contains Duplicate III 包含重复元素 III
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的更多相关文章
- [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 ...
- LeetCode 217. Contains Duplicate (包含重复项)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- Java实现 LeetCode 220 存在重复元素 III(三)
220. 存在重复元素 III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最 ...
- Leetcode 220.存在重复元素III
存在重复元素III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ. ...
- 【每日算法】存在重复元素 III
题目描述 这是 LeetCode 上的 220. 存在重复元素 III, 难度为 [中等] 给你一个整数数组 nums 和两个整数 k 和 t .请你判断是否存在 两个不同下标 i 和 j,使得 ab ...
- 从n个元素中选择k个的所有组合(包含重复元素)
LeetCode:Combinations这篇博客中给出了不包含重复元素求组合的5种解法.我们在这些解法的基础上修改以支持包含重复元素的情况.对于这种情况,首先肯定要对数组排序,以下不再强调 修改算法 ...
随机推荐
- CentOS6.5_x64上简单编译配置Heartbeat3.0.4
Heartbeat 3与 2.x的最大差别在于,3 按模块把的原来2.x 拆分为多个子项目,并且提供了一个cluster-glue的组件,专用于Local ResourceManager 的管理.即h ...
- 使用Arduino和SD卡模块记录数据并导出到Excel
在本篇文章中,我们将学习如何基于Arduino开发板使用一个SD卡模块.同时结合DS3231实时时钟模块,我们将制作一个数据记录仪的示例,在示例中,我们将温度传感器的数据存储到SD卡中,并将这些数据导 ...
- Spring Cloud Zuul网关(快速搭建)
zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架.相当于是设备和 Netflix ...
- python通过LXML库读取xml命名空间
xml实例版本: <a> <city:table xmlns:city="city"> <heilongjiang name="citys& ...
- 6、Python基础语法
一.Python输出 print是python输出的关键字,默认是输出内容后换行. 如果不想换行,需要在变量末尾加上 end="" . a = 'hello' b = 'world ...
- Jmeter扩展自定义函数
步骤1.导入lib\ext下ApacheJMeter_core.jar和ApacheJMeter_functions.jar 步骤2.新建function的类的package声明必须已".f ...
- 《三体》刘慈欣英文演讲:说好的星辰大海你却只给了我Facebook
美国当地时间2018日11月8日,著名科幻作家刘慈欣被授予2018年度克拉克想象力贡献社会奖(Clarke Award for Imagination in Service to Society),表 ...
- java 库存管理
第一种方法: import java.util.Scanner; import java.util.Random; class kuCun { //库存管理 public static void m ...
- div+css制作哆啦A梦
纯CSS代码加上 制作动画版哆啦A梦(机器猫) 哆啦A梦(机器猫)我们大家一定都很熟悉,今天给大家演示怎么用纯CSS.代码,来做一个动画版的哆啦A梦. 效果图: 下面代码同学可以查看一下,每个线条及椭 ...
- 第06组 Beta冲刺(1/4)
队名:福大帮 组长博客链接: https://www.cnblogs.com/mhq-mhq/p/11990568.html 作业博客 : https://edu.cnblogs.com/campus ...