要求

  • 给出整型数组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的更多相关文章

  1. 220. Contains Duplicate III

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

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

  3. 【medium】220. Contains Duplicate III

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

  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 包含重复元素 III

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

  6. [刷题] 219 Contains Duplicate II

    要求 给出整型数组nums和整数k,是否存在索引i和j,nums[i]==nums[j],且i和j之间的差不超过k 思路 暴力解法(n2) 建立最长为k+1的滑动窗口,用set查找窗口中是否有重复元素 ...

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

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

  9. 【LeetCode】220. Contains Duplicate III

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

随机推荐

  1. [状压DP]子矩阵

    子 矩 阵 子矩阵 子矩阵 题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵 如,下面左图中选取第 2 . 4 ...

  2. [Fundamental of Power Electronics]-PART I-2.稳态变换器原理分析-2.4 Cuk变换器实例

    2.4 Cuk 变换器 作为第二个示例,考虑图2.20(a)的变换器.该变换器执行类似于降压-升压变换器的直流转换功能:它可以增加或减小直流电压的幅值,并且可以反转极性.使用晶体管和二极管的实际实现如 ...

  3. 【Flutter 3-5】Flutter进阶教程——在Flutter中使用Lottie动画

    作者 | 弗拉德 来源 | 弗拉德(公众号:fulade_me) Lottie动画 在移动开发中总是需要展示一些动画特效,作为程序员的我们并不是很擅长用代码做动画,即便是有些动画可以实现,在跨平台的过 ...

  4. XSS-change通关历程

    Level1:没有过滤. <script>alert(1)</script> <svg/onload=alert(1)> <script>confirm ...

  5. HelloWorld的命令提示符运行和命名规则

    HelloWorld 随便新建一个文件夹,存放代码 新建一个Java文件 文件后缀名为.java Hello.java [注意点]系统可能没有显示文件后缀名,我们需要手动打开 编写代码 public ...

  6. HMS Toolkit助力开发者高效集成HMS Core

    当你的应用想集成华为HMS Core服务和上线华为应用市场,或当你已经开发了一个Android应用并集成了第三方移动服务,需要迁移使用HMS Core服务和上线华为应用市场的时候,如何快速.便捷.高效 ...

  7. 告别DNS劫持,一文读懂DoH

    如果评选一个差评服务器榜单,除去育碧高居榜首外,一定也少不了 Nintendo Switch 让人头秃的联网服务.尽管任天堂已经架设了香港 CDN 服务器用于加速,但是更新安装的速度也没有什么大幅改变 ...

  8. TP6学习笔记一:安装与基本配置

    1 说明与概述 1.1 说明 以下内容大部分来源于TP6完全开发手册,以手册为主附上个人理解,仅作学习使用. 1.2 概述 第一篇学习笔记,主要记录TP6的基础,包括TP6简介,安装,Hello Wo ...

  9. Workerman:PHP的socket框架

    hi,我们今天来讲讲Workerman,什么是Workerman呢? 看看官网上的介绍 Workerman是一款开源高性能异步PHP socket框架.支持高并发,超高稳定性,被广泛的用于手机app. ...

  10. TCP:与UDP区别、三次握手、四次挥手、Socket 编程

    1. TCP 基本认识 TCP 头部格式 为什么需要 TCP 协议?TCP 工作在哪一层? 什么是 TCP ? 什么是 TCP 连接? 如何唯一确定一个 TCP 连接呢? 有一个 IP 的服务器监听了 ...