[刷题] 220 Contains Duplicate III
要求
- 给出整型数组nums和整数k,是否存在索引i和j
- 使得nums[i]和nums[j]之差不超过t,且i和j之差不超过k
思路
- 建立k个元素的有序查找表
- 每次有新元素加入,寻找查找表中大于 nums[i]-t 的最小值,若存在且此值小于 nums[i]+t,则目标元素存在
- set底层是二叉树实现,时间(nlogn),空间(k)
- vector中的int值相加可能产生整型溢出,set中使用64位整数 long long
实现
1 class Solution {
2 public:
3 bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
4 if(t < 0)
5 return false;
6
7 set<long long> record;
8 for(int i = 0 ; i < nums.size() ; i ++){
9
10 if(record.lower_bound((long long)nums[i] - (long long)t) != record.end() &&
11 *record.lower_bound((long long)nums[i] - (long long)t ) <= (long long)nums[i] + (long long)t)
12 return true;
13
14 record.insert(nums[i]);
15
16 if(record.size() == k + 1)
17 record.erase( nums[i-k] );
18 }
19
20 return false;
21 }
22 };
[刷题] 220 Contains Duplicate III的更多相关文章
- 220. Contains Duplicate III
题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- 220. Contains Duplicate III 数组指针差k数值差t
[抄题]: Given an array of integers, find out whether there are two distinct indices i and j in the arr ...
- 【medium】220. Contains Duplicate III
因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器. Given an array of integers, find out whether there ar ...
- LeetCode 220. Contains Duplicate III (分桶法)
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [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 suc ...
- [刷题] 219 Contains Duplicate II
要求 给出整型数组nums和整数k,是否存在索引i和j,nums[i]==nums[j],且i和j之间的差不超过k 思路 暴力解法(n2) 建立最长为k+1的滑动窗口,用set查找窗口中是否有重复元素 ...
- Java for LeetCode 220 Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- (medium)LeetCode 220.Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 【LeetCode】220. Contains Duplicate III
题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...
随机推荐
- 学习笔记-vue.js获取file文件数据
在vue中file不能像其他input一样使用 v-model 双向数据绑定,因为文件选择是只读,只能用onchange监控值得变化. 所有需要使用v-on:change去监控. 例1: <in ...
- React 错误边界组件
这是React16的内容,并不是最新的技术,但是用很少被讨论,直到通过文档发现其实也是很有用的一部分内容,还是总结一下- React中的未捕获的 JS 错误会导致整个应用的崩溃,和整个组件树的卸载.从 ...
- Simulink中Scope数据保存至Workspace制图
0 问题 通常情况下,仿真模型中scope波形可编辑程度并不高,尽管高版本MATLAB中已经可以将其直接导出到figure,但效果并不是特别理想.在需要高质量输出波形图场合,就需要将其中数据导出到wo ...
- 2021 小白版,360 行行行转 IT
hey guys ,我是 cxuan,这一篇文章我就要和你聊聊编程如何学习,这一篇文章涉及的内容简直太多了,我将从入门开始,一步一步到如何提高,然后到一些学习的相关问题,还有一些计算机相关的术语等,干 ...
- 关于ArrayList 中子方法 -- contains 疑惑解决
写之前先看下 ArrayList 子函数 contains 的Api 怎么介绍: boolean contains(Object o) 如果此列表中包含指定的元素,则返回 true ...
- C# Linq 延迟查询的执行
在定义linq查询表达式时,查询是不会执行,查询会在迭代数据项时运行.它使用yield return 语句返回谓词为true的元素. var names = new List<string> ...
- ASP.NET CORE使用WebUploader对大文件分片上传,并通过ASP.NET CORE SignalR实时反馈后台处理进度给前端展示
本次,我们来实现一个单个大文件上传,并且把后台对上传文件的处理进度通过ASP.NET CORE SignalR反馈给前端展示,比如上传一个大的zip压缩包文件,后台进行解压缩,并且对压缩包中的文件进行 ...
- 各种平衡树收集(收集控(‐^▽^‐))\平衡树模板题的各种花式做法QAQ
非旋转treap!!!(FHQ Treap) 递归版Splay(无需维护父指针) Scapegoat _ Tree--替罪羊树(一只(棵)特立独行的猪(树)) 宗法树(平衡线段树\finger_tre ...
- Sublime插件安装和使用
Sublime插件安装和使用 插件安装的方式: 插件安装方式一:直接安装 下载插件安装包,然后把安装解压到packages目中,按成安装(菜单->首选项->浏览插件) 插件安装方法二:使用 ...
- Think5之删除单条数据功能
//删除单条学员信息 public function deleteStu(Request $request){ $stu_id = $request->param('id'); $result ...