[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种解法.我们在这些解法的基础上修改以支持包含重复元素的情况.对于这种情况,首先肯定要对数组排序,以下不再强调 修改算法 ...
随机推荐
- jquery 子元素筛选选择器
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...
- Kotlin注解深入解析与实例剖析
在上一次https://www.cnblogs.com/webor2006/p/11522798.html中学习了Kotlin注解相关的东东,这次继续对Kotlin的注解继续学习: 注解也可以拥有自己 ...
- POJ 1797 Heavy Transportation(Kruskal灵活使用)(瓶颈树)
题意: 求1到n路径上最大的最小值. 原因:样例输入 1 3 3 1 2 3 1 3 4 2 3 5 1-2最多可以运输3,2-3可最多以运输5,但是2的来源只有3,所以路径1-2-3上能运输的量为3 ...
- django 进行语言的国际化及在后台进行中英文切换
项目的部署地为: 中国大陆与美国东海岸, 两个地区的服务器数据不进行同步, 中国地区的服务器页面展示中文, 美国地区的服务器页面展示成英文, 项目后台使用python编程语言进行开发, 并结合djan ...
- linux中>/dev/null 2>&1和2>&1 > /dev/null
转载:https://www.cnblogs.com/520playboy/p/6275022.html 背景 我们经常能在shell脚本中发现>/dev/null 2>&1这样的 ...
- Django REST framework版本控制
参考链接:https://www.cnblogs.com/liwenzhou/p/10269268.html 1.路由: #版本控制 re_path('^(?P<version>[v1|v ...
- Combined beamformers for robust broadband regularized superdirective beamforming
[未完待续]结合波束形成器的鲁棒性宽带正则化超指向波束形成方法[1].用于宽带信号的波束形成方法.结合延时求和波束形成DSB以及超指向波束形成SDB方法,给定用户自定义的正则化因子,采用一个简单的参数 ...
- spring boot+Quartz+数据库存储
SpingBoot+Quartz+数据库存储 1.Spring整合Quartz 2.读取数据库中表达式启动定时任务1(每5s执行) 3.更改定时任务状态(启用/禁用),定时任务1停止 4.读取数据库中 ...
- learning java FileWriter
import java.io.FileWriter; import java.io.IOException; public class FileWriterTest { public static v ...
- pgloader 学习(四)一些简单操作例子
上边已经说明了pgloader 的基本使用(篇理论),但是对于实际操作偏少,以下是一个简单的操作 不像官方文档那样,我为了方便,直接使用docker-compose 运行,同时这个环境,会在后边大部分 ...