[刷题] 220 Contains Duplicate III
要求
- 给出整型数组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的更多相关文章
- 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 数组指针差k数值差t
[抄题]: Given an array of integers, find out whether there are two distinct indices i and j in the arr ...
- 【medium】220. Contains Duplicate III
因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器. Given an array of integers, find out whether there ar ...
- 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 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [刷题] 219 Contains Duplicate II
要求 给出整型数组nums和整数k,是否存在索引i和j,nums[i]==nums[j],且i和j之间的差不超过k 思路 暴力解法(n2) 建立最长为k+1的滑动窗口,用set查找窗口中是否有重复元素 ...
- 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 ...
随机推荐
- kubernetes 降本增效标准指南| 资源利用率提升工具大全
背景 公有云的发展为业务的稳定性.可拓展性.便利性带来了极大帮助.这种用租代替买.并且提供完善的技术支持和保障的服务,理应为业务带来降本增效的效果.但实际上业务上云并不意味着成本一定较少,还需适配云上 ...
- Linux就该这么学:重定向,管道符,通配符,转义符,环境变量
第三章:重定向,管道符,环境变量 3.1 输入输出重定向 定义:输入重定向是指将文件导入命令中,输出重定向是指将原本显示到屏幕的信息输出问文件.相较于输入重定向,输出重定向使用的更多,下面重点介绍输出 ...
- 为Github的README.md生成目录的小工具
1 概述 因为Github的README.md文件[TOC]不生效,因此封装了一个别人已封装好的JAR包供大家使用. 2 使用方法 用Java做的,只需要JDK11以上的环境: java -jar t ...
- https如何使用python+flask来实现
摘要:一般http中存在请求信息明文传输,容易被窃听截取:数据的完整性未校验,容易被篡改:没有验证对方身份,存在冒充危险.面对这些问题,怎么破? 一.为什么要用https 一般http中存在如下问题: ...
- matlab通用命令
常用命令 命令 命令说明 cd 显示或改变当前文件夹 dir 显示当前文件夹或指定目录下的文件 clc 清除工作窗中的所有显示内容 home 将光标移至命令行窗口的最左上角 clf 清除图形窗口 ty ...
- JDBC_12_JDBC事务
JDBC事务 JDBC中事务默认自动提交,每执行一次SQL就会自动提交一次. 这样的话可能出现数据安全性问题. connection.setAutoCommit(false) false代表关闭自动提 ...
- os shutil 模块
OS --- 操作系统接口 os.system(command) # 在python中执行系统指令 os.popen(command[, mode[, bufsize]]) #os.popen() 方 ...
- 用laravel Maatwebsite\Excel 教你导出漂亮的Excel表单
先来看效果图 还算是漂亮吧 哈哈哈. 第一步当然是安装包咯 环境要求 PHP: ^7.0Laravel: ^5.5 composer require maatwebsite/excel 目前3.1 只 ...
- 常用head标签
最小推荐 <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content= ...
- git pull 默认拉取远端其他分支 问题解决
今天工作中遇见了一个问题:执行git pull 命令时,默认合并了远端的某个分支,经过查阅资料发现是git的配置问题. 如图所示: git 查看远端主机详细配置信息 git remote show o ...