[刷题] 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 ...
随机推荐
- " "( 双引号) 与 ' '( 单引号) 差在哪?-- Shell十三问<第四问>
" "( 双引号) 与 ' '( 单引号) 差在哪?-- Shell十三问<第四问> 经过前面两章的学习,应该很清楚当你在 shell prompt 后面敲打键盘.直到 ...
- Java学习笔记--异常机制
简介 在实际的程序运行过程中,用户并不一定完全按照程序员的所写的逻辑去执行程序,例如写的某个模块,要求输入数字,而用户却在键盘上输入字符串:要求打开某个文件,但是文件不存在或者格式不对:或者程序运行时 ...
- A. 【例题1】数字反转
题目解析 字符串的基础操作,注意判断零即可 #include <bits/stdc++.h> using namespace std; int i; char c[15]; int mai ...
- [单调栈]Imbalanced Array
I m b a l a n c e d A r r a y Imbalanced Array ImbalancedArray 题目描述 You are given an array a a a con ...
- 010_Nginx入门
目录 使用场景 什么是Nginx 正向代理与反向代理 正向代理:代理客户端 反向代理:代理服务端 Nginx的作用 反向代理 负载均衡 轮询 加权轮询 IP hash 动静分离 Windows下安装 ...
- Spring Boot自动配置原理
使用Spring Boot之后,一个整合了SpringMVC的WEB工程开发,变的无比简单,那些繁杂的配置都消失不见了,这 是如何做到的? 一切魔力的开始,都是从我们的main函数来的,所以我们再次来 ...
- JavaWeb 补充(JSP&EL&JSTL)
1. JSP: 1. 指令 2. 注释 3. 内置对象 2. MVC开发模式 3. EL表达式 4. JSTL标签 5. 三层架构 JSP: 1. 指令 * 作用:用于 ...
- 编写shell脚本让springboot项目在CentOS中开机自启动
springboot项目部署在CentOS系统上时,如果遇到停电关机,公司的实施人员就得跑到甲方现场重新启动项目并测试,很是麻烦,这里探讨如何编写shell脚本控制springboot项目开机时自动启 ...
- 【CPU100%排查】CPU100%问题排查方案
1.使用top -c 查看CPU 占用情况 ,按P(大写)可以倒序查看占CPU占用率 2.找到占用率高的进程以后,再定位到具体线程 比如 此时进程ID 14724 CPU占用高,进一步使用top - ...
- Python中的Pandas模块
目录 Pandas Series 序列的创建 序列的读取 DataFrame DataFrame的创建 DataFrame数据的读取 Panel Panel的创建 Pandas Pandas ( Py ...