给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ。

示例 1:

输入: nums = [1,2,3,1], k = 3, t = 0 输出: true

示例 2:

输入: nums = [1,0,1,1], k = 1, t = 2 输出: true

示例 3:

输入: nums = [1,5,9,1,5,9], k = 2, t = 3 输出: false

方法一:暴力

方法二:

因为c++的map和set都是用搜索二叉树建立的

所以可以用map或者set

class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t)
{
set<long long> s;
for(int i = 0; i < nums.size(); i++)
{
set<long long> :: iterator itr = s.lower_bound((long long)nums[i] - t);
if(itr != s.end() && abs((long long)nums[i] - *itr) <= t)
{
return true;
}
s.insert(nums[i]);
if(s.size() > k)
s.erase(nums[i - k]);
}
return false;
}
};

Leetcode220. Contains Duplicate III存在重复元素3的更多相关文章

  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. [LeetCode] 219. Contains Duplicate II 包含重复元素 II

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  3. [LeetCode] 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] 219. Contains Duplicate II ☆(存在重复元素2)

    每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...

  5. LeetCode Contains Duplicate (判断重复元素)

    题意: 如果所给序列的元素不是唯一的,则返回true,否则false. 思路: 哈希map解决. class Solution { public: bool containsDuplicate(vec ...

  6. 220 Contains Duplicate III 存在重复 III

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

  7. [LeetCode] 217. Contains Duplicate 包含重复元素

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  8. Leetcode 220.存在重复元素III

    存在重复元素III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ. ...

  9. POJ 3916:Duplicate Removal 将相近的重复元素删除

    Duplicate Removal Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1745   Accepted: 1213 ...

随机推荐

  1. 判断Paging File 的方法

    当前环境,MiniFilter 1:FsRtlIsPagingFile 参数是一个 FileObject 2:判断操作标识   SL_OPEN_PAGING_FILE FlagOn 宏可以直接做到,传 ...

  2. Ascii码 、16进制与 char

            对于一个非计算机专业出身的人,以前只知道计算机中所有的数据都是以二进制形式进行存储,计算,通信的.但是人类文明中,主要的信息展现以文本的形式展现的.如果使用内存中的0和1来表示文本一直 ...

  3. Python全栈开发:web框架们

    Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. 1 2 3 ...

  4. 01.MyBatis快速入门

    1.下载jar包 Mybatis包+数据库驱动包 https://github.com/mybatis/mybatis-3/releases 2.新建Java工程并导入jar包 3.创建数据库与表 C ...

  5. 数据结构_冒泡排序(python)

    1.核心思想:比较两个元素,如果前一个比后一个大则进行交换,经过对每个元素的比较,最后最大的元素被放在在最后位置 操作方法: 外层正常for循环遍历,到n-1位,内层for循环相邻两个数比较大小,小数 ...

  6. 模拟+贪心——cf1131E

    超级恶心的题,写了好久,直接倒序模拟做,但是网上有博客好像是直接正序dp做的.. 因为左端点和右端点是永远不会变的,然后情况要考虑全 /* 从后往前插 只要记录左连续,右连续,中间连续 左端点一定是L ...

  7. .git文件夹太大问题及解决方法

    最近我们做了自动化构建, 发现文件.git文件夹越来越大, 求后端小伙伴帮忙, 小伙伴指点了一下说周末弄了一下, 忘记命令的.大致的意思就是找到git 提交了哪些大文件. 然后重构git, 先分享给小 ...

  8. memcache课程---1、memcache介绍及安装(memcache作用)

    memcache课程---1.memcache介绍及安装(memcache作用) 一.总结 一句话总结: 减少对数据库的访问,因为数据库的访问比较花费时间 1.memcache为什么比操作数据库快的多 ...

  9. codeforces 1136E-Nastya Hasn't Written a Legend

    传送门:QAQQAQ 题意:有一个数组a和一个数组k,数组a一直保持一个性质:a[i + 1] >= a[i] + k[i].有两种操作:1,给某个元素加上x,但是加上之后要保持数组a的性质.比 ...

  10. Python学习day08-python进阶(2)-内置方法

    Python学习day08-python进阶(2)-内置方法 列表数据类型内置方法 作用 描述多个值,比如爱好 定义方法       xxxxxxxxxx 2         1 hobby_list ...