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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. (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 ...

  4. 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 ...

  5. 【LeetCode】220. Contains Duplicate III

    题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  6. 220. Contains Duplicate III

    题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  7. 220 Contains Duplicate III 存在重复 III

    给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 nums [i] 和 nums [j] 的绝对差值最大为 t,并且 i 和 j 之间的绝对差值最大为 k. 详见:https://le ...

  8. 【medium】220. Contains Duplicate III

    因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器. Given an array of integers, find out whether there ar ...

  9. 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 ...

随机推荐

  1. spring boot高性能实现二维码扫码登录(下)——订阅与发布机制版

     前言 基于之前两篇(<spring boot高性能实现二维码扫码登录(上)——单服务器版>和<spring boot高性能实现二维码扫码登录(中)——Redis版>)的基础, ...

  2. C语言中数据类型的取值范围

    C语言中数据类型的取值范围如下:char -128 ~ +127 (1 Byte)short -32767 ~ + 32768 (2 Bytes)unsigned short 0 ~ 65536 (2 ...

  3. Java基础笔记(1)----语言基础

    变量 变量:是内存中的一块存储空间,是存储数据的基本单元. 使用:先声明,后赋值,在使用. 声明:数据类型 + 变量名 = 值.(例:int a = 5:) 数据类型 分类:如图: 详解: Strin ...

  4. Linux下的进程与线程(一)—— 进程概览

    进程是操作系统分配资源的基本单位.线程是操作系统进行运行和调度的基本单位. 进程之间可以切换,以便轮流占用CPU,实现并发.一般进程运行在用户模式下,只能执行指令集中的部分指令. 当进程进行上下文切换 ...

  5. python全栈学习--day2

    一.in的使用 说明:in有相当多的用处,比如判断,循环for 等. 实例一:in 操作符用于判断关键字是否存在于变量中 s = '男人john' print('男孩' in s) print('男孩 ...

  6. 使用idea新建jsp

    使用idea解决新建jsp文件而找不到jsp文件模版的新建选项,这样每次创建一个新的jsp文件岂不是很耗时间? 解决办法: 就是要让idea知道你需要在这个目录下创建jsp文件 左上角,file中点击 ...

  7. Beta冲刺计划---Day0

    Beta阶段报告---Day0 1.需要改进完善的功能   我们上一阶段开发由于开发时间匆忙,对于爬虫耗时的优化没有考虑.优化的空间我在Alpha阶段的总结报告里说过,具体看下图.   这张图显示出爱 ...

  8. 冲刺NO.11

    Alpha冲刺第十一天 站立式会议 项目进展 项目进入尾声,主要测设工作完成过半,项目总结也开始进行. 问题困难 项目的困难现阶段主要是测试过程中存在一些"盲点"很难发现或者发现后 ...

  9. 团队作业7——第二次项目冲刺(Beta版本12.08)

    项目每个成员的进展.存在问题.接下来两天的安排. 已完成的内容:完成了排行榜的测试.上传头像功能的原型设计.界面优化 计划完成的内容:上传头像功能开发.测试.头像裁剪原型设计 每个人的工作 (有wor ...

  10. C++智能指针(auro_ptr...)

    写的很好,忍不住转了: 博文原址:http://blog.csdn.net/xt_xiaotian/article/details/5714477 一.简介 由于 C++ 语言没有自动内存回收机制,程 ...