LeetCode(220) Contains Duplicate III
题目
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k.
分析
题目描述:给定一个整数序列,查找是否存在两个下标分别为i和j的元素值|nums[i]−nums[j]|<t且满足i于j的距离最大为k;
开始的时候,采取同 LeetCode(219) Contains Duplicate II的方法,元素值|nums[i]−nums[j]|<t的元素在set内遍历查找一遍,复杂度为O(n),很遗憾的超时了;
然后,查找资料,看到了另外一种简单的方法map解决方法:
使用map数据结构来解,用来记录数字和其下标之间的映射。 这里需要两个指针i和start,刚开始i和start都指向0,然后i开始向右走遍历数组,如果i和start之差大于k,且map中有nums[start],则删除并start加一。这样保证了map中所有的数的下标之差都不大于k,然后我们用map数据结构的lowerbound()函数来找一个特定范围,就是大于或等于nums[i]−t的地方,所有小于这个阈值的数和nums[i]的差的绝对值会大于t (可自行带数检验)。然后检测后面的所有的数字,如果数的差的绝对值小于等于t,则返回true。最后遍历完整个数组返回false。
AC代码
class Solution {
public:
//方法一,TLE
bool containsNearbyAlmostDuplicate1(vector<int>& nums, int k, int t) {
if (nums.empty())
return false;
int sz = nums.size();
//使用容器unordered_set 其查找性能为常量
unordered_set<int> us;
int start = 0, end = 0;
for (int i = 0; i < sz; ++i)
{
int len = us.size();
for (int j = 0; j < len; ++j)
{
int tmp = abs(nums[j] - nums[i]);
if (tmp <= t)
return true;
}//for
us.insert(nums[i]);
++end;
if (end - start > k)
{
us.erase(nums[start]);
++start;
}
}//for
return false;
}
//方法二:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if (nums.empty())
return false;
int sz = nums.size();
map<long long, int > m;
int start = 0;
for (int i = 0; i < sz; ++i)
{
if (i - start >k && m[nums[start]] == start)
m.erase(nums[start++]);
auto a = m.lower_bound(nums[i] - t);
if (a != m.end() && abs(a->first - nums[i]) <= t)
return true;
//将元素值和下标插入map
m[nums[i]] = i;
}//for
return false;
return false;
}
};
LeetCode(220) Contains Duplicate III的更多相关文章
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- LeetCode(217)Contains Duplicate
题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...
- LeetCode(260) Single Number III
题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
随机推荐
- Jenkins+Gitlab+Ansible自动化部署(四)
接Jenkins+Gitlab+Ansible自动化部署(三)https://www.cnblogs.com/zd520pyx1314/p/10235394.html Jenkins应用 Jenkin ...
- ECShop怎么首页调用文章列表
举例如首页调用方法:1.先打开index.php文件找到以下代码:$smarty->assign('new_articles', index_get_new_articles()); // 最新 ...
- 零基础逆向工程21_PE结构05_数据目录表_导出表
数据目录 1.我们所了解的PE分为头和节,在每个节中,都包含了我们写的一些代码和数据,但还有一些非常重要 的信息是编译器替我们加到PE文件中的,这些信息可能存在在任何可以利用的地方. 2.这些信息之所 ...
- 带你零基础入门redis【二】
本篇文章介绍redis如何设置开机自启动以及如何在java中应用 一.设置redis开机自启 1.修改redis配置 [root@VM_6_102_centos ~]# vim /usr/local/ ...
- 一键部署joomla开源内容管理平台
https://market.azure.cn/Vhd/Show?vhdId=10896&version=12949 产品详情 产品介绍Joomla是一套自由.开放源代码的内容管理系统,以PH ...
- Unity runtime性能分析器
一. Profiler: 1. CPU Usage A. WaitForTargetFPS: Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间 B. Overhead: Profiler总体 ...
- Processing一些常用技巧
一些常用技巧总结: Tweak模式 快速查找函数用法 显示与输入中文注释 代码快速对齐 批量添加注释符 Tweak模式 Tweak模式是非常有用的功能,自3.0版本后,它就正式整合到Processin ...
- Paper: TranE
论文标题:Translating Embeddings for Modeling Multi-relational Data 标题翻译:多元关系数据翻译嵌入建模 摘要: 考虑多元关系数据的实体和关系在 ...
- 绘制方式和OpenGL枚举对应关系
绘制方式和OpenGL枚举对应关系 图元类型 OpenGL枚举量 点 GL_POINTS 线 GL_LINES 条带线 GL_LINE_STRIP 循环线 GL_LINE_LOOP 独立三角形 GL_ ...
- Vmware 安装CentOS7时连不上网问题的解决
在VmWare 上安装Centos7时,装好vmware后还是连不上网,通过查找资料原来是因为有线网卡没有激活,默认centos和redhat7都是不启用有线网卡的,要么手动开启,要么安装时直接启用! ...