[Leetcode] 220. Contains Duplicate 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.
题目大意:给定一个整数数组nums,找出是否存在:两个不同的下标(索引index)i 和 j ,使得nums[i] 和 nums[j]的差的绝对值小于等于t,i 和 j 的差的绝对值小于等于k。
思路:建立一个set数据类型,把 i 之前k个数存入set中,set默认数据是从小到大排序的。
对于i,j 时,若符合条件,则有 | nums[j] - nums[i] |<=t,则-t <= nums[j] - nums[i] <= t,因此有nums[j]>=num[i] - t,因此使用lower_bound函数获取set中大于等于num[i] - t的第一个元素,若存在该元素(it!=s.end()),则再判断 nums[j] - nums[i] <= t是否成立,需要注意的是整数可能溢出。
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
set<int> s;
for(int i=0;i<nums.size();i++)
{
//if(s.size()==k) s.erase(nums[i-k]);//错误!
if(i-k-1>=0) s.erase(nums[i-k-1]);//当i>k+1时,就一直删除最先存入set里面的数
auto it = s.lower_bound(nums[i]-t);
if(it!=s.end()&&((long)*it-(long)nums[i])<=t) return true;
s.insert(nums[i]);
}
return false;
}
};
[Leetcode] 220. Contains Duplicate III的更多相关文章
- [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 ...
- 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 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 ...
- 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 存在重复 III
给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 nums [i] 和 nums [j] 的绝对差值最大为 t,并且 i 和 j 之间的绝对差值最大为 k. 详见:https://le ...
- 【medium】220. Contains Duplicate III
因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器. Given an array of integers, find out whether there ar ...
- 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 ...
随机推荐
- python变量定义和定义规范
变量定义的规则: 变量名只能是 字母.数字或下划线的任意组合 变量名的第一个字符不能是数字 以下关键字不能声明为变量名['and', 'as', 'assert', 'break', 'class', ...
- Go实现海量日志收集系统(一)
项目背景 每个系统都有日志,当系统出现问题时,需要通过日志解决问题 当系统机器比较少时,登陆到服务器上查看即可满足 当系统机器规模巨大,登陆到机器上查看几乎不现实 当然即使是机器规模不大,一个系统通常 ...
- Redis数据过期策略
1.Redis中key的的过期时间 通过EXPIRE key seconds命令来设置数据的过期时间.返回1表明设置成功,返回0表明key不存在或者不能成功设置过期时间.在key上设置了过期时间后ke ...
- java.lang.Thread、java.lang.ThreadGroup和java.lang.ThreadLocal<T>详细解读
一.Thread类 public class Thread extends Object impments Runnable 线程是程序中的 执行线程.java虚拟机允许应用程序并发地运行多个执行线 ...
- oracle中事务处理--事务隔离级别
概念:隔离级别定义了事务与事务之间的隔离程度. ANSI/ISO SQL92标准定义了一些数据库操作的隔离级别(这是国际标准化组织定义的一个标准而以,不同的数据库在实现时有所不同). 隔离级别 脏读 ...
- python全栈开发-Day10 装饰器(闭合函数的应用场)
一. 装饰器 装饰器就是闭包函数的一种应用场景 什么是闭包函数?我们再来回忆一下: 闭包函数: 定义在函数内部的函数,并且该函数包含对外部函数作用域(强调:对全局作用域名字的引用不算闭包)名字的引用, ...
- Python开发简单爬虫(二)---爬取百度百科页面数据
一.开发爬虫的步骤 1.确定目标抓取策略: 打开目标页面,通过右键审查元素确定网页的url格式.数据格式.和网页编码形式. ①先看url的格式, F12观察一下链接的形式;② 再看目标文本信息的标签格 ...
- 2018上C语言程序设计(高级)作业- 初步计划
C语言程序设计(高级)36学时,每周4学时,共9周.主要学习指针.结构和文件三部分内容.整个课程作业计划如下: PTA和博客的使用指南 若第一次使用PTA和博客,请务必先把PTA的使用简介和教师如何在 ...
- python API的安全认证
我们根据pid加客户端的时间戳进行加密md5(pid|时间戳)得到的单向加密串,与时间戳,或者其它字段的串的url给服务端. 服务端接收到请求的url进行分析 客户端时间与服务端的时间戳之差如果大于规 ...
- 2017-2018-1 我爱学Java 第一周 作业
构建之法 成员及分工 内容简介 作者简介 分章学习及问题 第一章 概论 第二章 个人技术和流程 第三章 软件工程师的成长 第四章 两人合作 第五章 团队和流程 第六章 敏捷流程 第七章 实战中的软件工 ...