Leetcode220. Contains Duplicate III存在重复元素3
给定一个整数数组,判断数组中是否有两个不同的索引 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的更多相关文章
- [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 ...
- [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 ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- LeetCode Contains Duplicate (判断重复元素)
题意: 如果所给序列的元素不是唯一的,则返回true,否则false. 思路: 哈希map解决. class Solution { public: bool containsDuplicate(vec ...
- 220 Contains Duplicate III 存在重复 III
给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 nums [i] 和 nums [j] 的绝对差值最大为 t,并且 i 和 j 之间的绝对差值最大为 k. 详见:https://le ...
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- Leetcode 220.存在重复元素III
存在重复元素III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ. ...
- POJ 3916:Duplicate Removal 将相近的重复元素删除
Duplicate Removal Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1745 Accepted: 1213 ...
随机推荐
- R:ggplot2数据可视化——进阶(2)
Part 2: Customizing the Look and Feel, 更高级的自定义化,比如说操作图例.注记.多图布局等 # Setup options(scipen=999) librar ...
- 笔记:Python操作sql
python操作mysql步骤: 创建connect连接 conn = connect(host='127.0.0.1', port=3306, user='root', password='1234 ...
- Linux安装Java与Eclipse
Linux安装Java和Eclipse 一.准备工作 1.下载jdk https://www.oracle.com/technetwork/java/javase/downloads/jdk8-do ...
- python类的静态方法和类方法区别
先看语法,python 类语法中有三种方法,实例方法,静态方法,类方法. # coding:utf-8 class Foo(object): """类三种方法语法形式&q ...
- SQLite C++操作种
SQLite C++操作类 为了方便SQLite的使用,封装了一个SQLite的C++类,同时支持ANSI 和UNICODE编码.代码如下: 头文件(SQLite.h) /************ ...
- 移动端,fixed bottom问题
//不显示 .bar { position:fixed; bottom:0; z-index:99; } //显示 .bar{ position:fixed; bottom:calc(90vh); / ...
- linux ssh密钥认证, 免密码登陆
1. 客户端生成密钥 # mkdir ~/.ssh # chmod ~/.ssh # cd ~/.ssh 生成RSA密钥 # ssh-keygen -t rsa (然后连续三次回车) 2. 把公钥传到 ...
- Mac 下搭建vue开发环境
tips:一定要有翻墙工具如lanter,另外要保证网速OK. 1. 首先需要安装homebrew liukingdeMBP:~ liuking$ /usr/bin/ruby -e "$(c ...
- day 38 MySQL之单表查询
MySQL之单表查询 阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER ...
- 2019牛客暑假多校赛(第二场) F和H(单调栈)
F-Partition problem https://ac.nowcoder.com/acm/contest/882/F 题意:输入一个数n,代表总共有2n个人,然后每个人对所有人有个贡献值,然后问 ...