给定一个整数数组,判断数组中是否有两个不同的索引 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. 2019 CCPC 湖南全国邀请赛

    A. Chessboard 做法1 单纯形. 做法2 最大费用可行流问题,行列模型. 对每行建一个点,每列建一个点.物品 \(i\) 在 \((r,c)\),那么 \(r\) 向 \(c\) 连流量为 ...

  2. UBOOT的的 C 语言代码部分

    调用一系列的初始化函数 1. 指定初始函数表: init_fnc_t *init_sequence[] = { cpu_init,           /* cpu 的基本设置         */ ...

  3. mac brew 安装 php 环境

    548  brew search php 549  brew tap homebrew/dupes 550  brew tap josegonzalez/homebrew-php 551  brew ...

  4. c# 中反射里的invoke方法的参数

    一个最简单的C#反射实例,首先编写类库如下: namespace ReflectionTest { public class WriteTest { //带参数的公共方法 public void Wr ...

  5. leetcode-137-只出现一次的数字②

    题目描述: 方法一:数学 class Solution: def singleNumber(self, nums: List[int]) -> int: return (sum(set(nums ...

  6. Struts2转换器

    为什么进行类型转换 在基于HTTP协议的Web应用中 客户端请求的所有内容都以文本编码方式传输到服务器端 服务器端的编程语言却有着丰富的数据类型 继承StrutsTypeConverter抽象类 继承 ...

  7. [COCI2019] Mobitel

    题目 显然不小于\(n\)这个东西我们不是很好搞,考虑正难则反,求出有多少条路径小于\(n\),之后拿\(C_{n+m}^m\)一减就好了 于是状态为\(dp[i][j][k]\)表示到\((i,j) ...

  8. linux 备份最近一天的文件

    #!/bin/bash #备份在最近一天修改的文件 #date 获取日期 +%Y-%m-%d 设置日期格式为yyyy-mm-dd的形式 BACKFILE=backup-$(date +%Y-%m-%d ...

  9. 项目无法依赖Springboot打出的jar

    1.原因 因为springboot-maven-plugin打包的第一级目录为Boot-INF,无法引用 2.解决 不能使用springboot项目自带的打包插件进行打包 <build> ...

  10. Maven编译资源文件拷贝

    <build> <finalName>op-balance-job-service</finalName> <plugins> <plugin&g ...