要求

  • 给出整型数组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. [Fundamental of Power Electronics]-PART II-7. 交流等效电路建模-7.3 脉冲宽度调制器建模

    7.3 脉冲宽度调制器建模 我们现在已经达成了本章开始的目标,为图7.1推导了一个有效的等效电路模型.但仍存在一个细节,对脉冲宽度调制(PWM)环节进行建模.如图7.1所示的脉冲宽度调制器可以产生一个 ...

  2. docker部署skywalking

    https://www.cnblogs.com/xiao987334176/p/13530575.html

  3. spring-cloud-gateway 服务网关

    Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关.网关作为流量的,在微服务系统中有着非常作用,网关常见的功能有路由转发.权限校验.限流控制等 ...

  4. 前端面试回顾(1)---javascript的面向对象

    前言 前一阵面试,过程中发现问到一些很基础的问题时候,自己并不能很流畅的回答出来.或者遇到一些基础知识的应用,由于对这些点理解的不是很深入,拿着笔居然什么都写不出来,于是有了回顾一下这些基础知识的想法 ...

  5. 为Github的README.md生成目录的小工具

    1 概述 因为Github的README.md文件[TOC]不生效,因此封装了一个别人已封装好的JAR包供大家使用. 2 使用方法 用Java做的,只需要JDK11以上的环境: java -jar t ...

  6. Matrix Chain Multiplication UVA - 442

    Suppose you have to evaluate an expression like ABCDE where A,B,C,D and E are matrices. Since matrix ...

  7. 【Azure 应用服务】App Service/Azure Function的出站连接过多而引起了SNAT端口耗尽,导致一些新的请求出现超时错误(Timeout)

    问题描述 当需要在应用中有大量的出站连接时候,就会涉及到SNAT(源地址网络转换)耗尽的问题.而通过Azure App Service/Function的默认监控指标图表中,却没有可以直接查看到SNA ...

  8. spring data jpa之Auditing 表的创建时间,更新时间自动生成策略

    java实际编程中,几乎每一张表都会有createTime和updateTime字段,spring的优秀之处在于只要用几个注解,就帮我们解决该类问题,具体实现: 1,实体类添加注解: @EntityL ...

  9. 基于MVC框架的JavaWeb网站开发demo项目(JSP+Servlet+JavaBean)

    1.环境配置 Windows10+Eclipse2020+jdk8+Tomcat9+MySQL8+Navicat10 2.需求分析 ①用户登录注册注销(查找.增加) ②显示用户列表(查找) ③显示用户 ...

  10. java面试一日一题:讲下mysql中的undolog

    问题:请讲下mysql中undo log的作用 分析:mysql中有很多日志,例,bin log undo log redo log,要弄清楚这些日志的作用,就要了解这些日志出现的背景及要解决的问题: ...