当前位置:网站首页>力扣(LeetCode)220. 存在重复元素 III(2022.08.08)
力扣(LeetCode)220. 存在重复元素 III(2022.08.08)
2022-08-09 10:28:00 【ChaoYue_miku】
给你一个整数数组 nums 和两个整数 k 和 t 。请你判断是否存在 两个不同下标 i 和 j,使得 abs(nums[i] - nums[j]) <= t ,同时又满足 abs(i - j) <= k 。
如果存在则返回 true,不存在返回 false。
示例 1:
输入:nums = [1,2,3,1], k = 3, t = 0
输出:true
示例 2:
输入:nums = [1,0,1,1], k = 1, t = 2
输出:true
示例 3:
输入:nums = [1,5,9,1,5,9], k = 2, t = 3
输出:false
提示:
0 <= nums.length <= 2 * 104
-231 <= nums[i] <= 231 - 1
0 <= k <= 104
0 <= t <= 231 - 1
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/contains-duplicate-iii
方法一:滑动窗口 + 有序集合
C++提交内容:
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
int n = nums.size();
set<int> rec;
for (int i = 0; i < n; i++) {
auto iter = rec.lower_bound(max(nums[i], INT_MIN + t) - t);
if (iter != rec.end() && *iter <= min(nums[i], INT_MAX - t) + t) {
return true;
}
rec.insert(nums[i]);
if (i >= k) {
rec.erase(nums[i - k]);
}
}
return false;
}
};
边栏推荐
猜你喜欢
随机推荐
踩坑scrollIntoView
MySQL执行过程及执行顺序
循环嵌套以及列表的基本操作
Dialogue with the DPO of a multinational consumer brand: How to start with data security compliance?See you on 8.11 Live!
OpenGL ES2.0编程三部曲(转载自MyArrow)
【Linux】宝塔面板设置MySQL慢查询日志,未走索引日志
tuple dictionary collection
shell脚本实战(第2版)/人民邮电出版社 脚本1 在PATH中查找程序
蓄电池建模、分析与优化(Matlab代码实现)
shell脚本实战(第2版)/人民邮电出版社 脚本2 验证输入:仅限字母和数字
day16_03集合
MySQL和MyEclipse的数据库连接操作
OpenGL 2.0编程例子
unix环境编程 第十五章 15.10 POSIX信号量
【原创】解决阿里云oss-browser.exe双击没反应打不开,提供一种解决方案
函数二
技术分享 | 如何模拟真实使用场景?mock 技术来帮你
xmms已经发布到v1.3了,好久没写博客了
Collections and Functions
2021-01-11-雪碧图做表情管理器