要求

  • 给出整型数组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. Python脚本打包成exe执行文件

    需求 一个教辅目录结构检查工具,目录结构是[书籍]-[章节]-[题目|答案]-[*.jpg],后台有个异步处理的服务,需要强依赖这个目录结构. 书籍解析是单独的pipeline,日志对用户不可见,这里 ...

  2. AttributeError: 'str' object has no attribute 'lowerr' Python常见错误

    方法名拼写错误 检查方法名拼写,如有错误改正即可 特别注意m和n

  3. [Fundamental of Power Electronics]-PART I-6.变换器电路-6.3 变压器隔离

    6.3 变压器隔离 在许多应用场合中,期望将变压器结合到开关变换器中,从而在变换器的输入输出之间形成直流隔离.例如,在离线(off-line)应用中(变换器输入连接到交流公用系统),根据监管部门要求, ...

  4. java面试-synchronized底层实现机制

    一.synchronized的三种应用方式 1.修饰实例方法,锁是当前实例对象,进入同步代码前要获得当前实例的锁 /** * synchronized修饰实例方法,当前线程的锁是实例对象account ...

  5. Java面向对象OOP思想概述

    目录 OOP思想(Object Oriented Programming) 类和对象 接口 抽象类 OOP三大特性 封装 继承 多态 OOP复用的形式 OOP思想(Object Oriented Pr ...

  6. [源码分析] 分布式任务队列 Celery 多线程模型 之 子进程

    [源码分析] 分布式任务队列 Celery 多线程模型 之 子进程 目录 [源码分析] 分布式任务队列 Celery 多线程模型 之 子进程 0x00 摘要 0x01 前文回顾 1.1 基类作用 1. ...

  7. JAVAEE_Servlet_05_ServletConfig接口

    ServletConfig接口 研究javax.servlet.ServletConfig接口 1.javax.servlet.ServletConfig是一个接口 2.Apache Tomcat服务 ...

  8. ReentrantLock理解

    原文出处:http://www.yund.tech/zdetail.html?type=1&id=ef94715a2838f06ab03b8621c23d1613 作者:jstarseven ...

  9. kubernetes的架构

    kubernetes架构 k8s的物理结构是master/node模式,架构图如下所示 master一般是三个节点或者五个节点做高可用,根据集群规模来定,master高可用指的是对apiserver做 ...

  10. MSSQL数据库清理Log 压缩数据库日志文件

    dump transaction 数据库名 with no_log go dbcc shrinkdatabase(数据库名)