给定一个整数数组,判断数组中是否有两个不同的索引 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. java笔试之提取不重复的整数

    输入一个int型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数. 此题可以使用linkedHashedSet\ArrayList\Stack\数组等来做.类似题目是输入一个数/字符串,从 ...

  2. UMP系统架构 RabbitMQ

  3. JS数组 二维数组 二维数组的表示 方法一: myarray[ ][ ];方法二:var Myarr = [[0 , 1 , 2 ],[1 , 2 , 3, ]]

    二维数组 一维数组,我们看成一组盒子,每个盒子只能放一个内容. 一维数组的表示: myarray[ ] 二维数组,我们看成一组盒子,不过每个盒子里还可以放多个盒子. 二维数组的表示: myarray[ ...

  4. LoadRunner内部结构(1)

    LoadRunner内部结构(1) 根据http://www.wilsonmar.com/1loadrun.htm  翻译: LoadRunner内部结构 1,            被测系统是由驱动 ...

  5. leetcode-第5周双周赛-1135-最低成本联通所有城市

    方法一: class Solution: def minimumCost(self, N: int, conections: List[List[int]]) -> int: def find( ...

  6. leetcode146周赛-5130-等价多米诺骨牌对的数量

    题目描述: 方法一: class Solution(object): def numEquivDominoPairs(self, dominoes): """ :type ...

  7. Python3基础笔记_字符串类型

    # 1.Python转义字符 a = "sqwerdf" # 2.Python字符串运算符 ''' + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符 ...

  8. 漫说安全|智能的云WAF,开挂的Web防御

    “漫说安全”是我们推出的一个新栏目,以简洁明了的形式展现高深晦涩的云安全. 今天我们要讲的是智能的云WAF到底有啥“本领”,答案就在漫画里^_^ 漫画看完后估计你还会有些小疑问,不要着急,安全君特意准 ...

  9. thinkphp 动态查询

    借助PHP5语言的特性,ThinkPHP实现了动态查询,核心模型的动态查询方法包括下面几种: 方法名 说明 举例 getBy 根据字段的值查询数据 例如,getByName,getByEmail ge ...

  10. springboot导入excel到mysql

    @Controller @RequestMapping(path = "/excel") public class ImportController { @Autowired pr ...