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 ...
随机推荐
- 主页面与iframe页面之间的javascript函数的调用
1:在主页面里调用iframe页里面的javascript函数 <script type="text/javascript"> var childWindow = $( ...
- open 和 release
我们开始在真实的 scull 函数中使用它们. open 方法 open 方法提供给驱动来做任何的初始化来准备后续的操作. 在大部分驱动中, open 应当 进行下面的工作: 检查设备特定的错误( ...
- h5 上下左右前后居中
.outer { width: 200px; height: 200px; background: red; position: relative; } .inner { position: abso ...
- SaltStack远程执行Windows job程序(黑窗口)填坑经过
近期接到领导通知,要将公司内的所有Windows服务添加到自动发布系统中,由于这种服务很多,节点分布散乱,每次都是由开发主管手动替换(虽然他们自己开发了自动打包替换工具,但仍需要一台一台登陆到服务器上 ...
- Less适配移动端rem
@ue-width: 750; /* 设计图的宽度 */ .px2rem(@px) { @remValue: @px/@ue-width*10; @pxToRem: ~"@{remValue ...
- 我王某的 低错&&(女装)Flag
日月共鉴,人心同睹. 喜欢你. 喜欢你的眼睛. 春天银河般闪烁的双瞳.春日阳光般温柔的眼神. 喜欢你的头发. 在微风中飘拂丝绢般柔滑的长发. 喜欢你的嘴唇. 给我甜蜜的吻.吐出忧伤叹息的嘴唇. 喜欢你 ...
- Composer的简介说明与安装
https://mp.weixin.qq.com/s/aSZRhoa2-JjKOTRVhPdxdQ Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为 ...
- System.Web.Mvc.HttpUnauthorizedResult.cs
ylbtech-System.Web.Mvc.HttpUnauthorizedResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neut ...
- jquery 判断当前设备是PC端还是移动端
$(function(){ var system = { win: false, mac: false, xll: false, ipad:false }; //检测平台 var p = naviga ...
- mongdb 使用聚合函数异常
异常信息: Command execution failed: Error [The 'cursor' option is required, except for aggregate with t ...