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 ...
随机推荐
- 熔断 降级(polly)
熔断 降级(polly) https://www.cnblogs.com/szlblog/p/9300845.html1.熔断降级的概念: 熔断:我这里有一根长度一米的钢铁,钢铁的熔点1000度(假设 ...
- NET Core 2.1 Preview 1
NET Core 2.1 Preview 1 [翻译] .NET Core 2.1 Preview 1 发布 原文: Announcing .NET Core 2.1 Preview 1 今天,我们宣 ...
- webpack.config.js====插件clean-webpack-plugin
1. 安装:主要是用来清除重复文件,生成最新的的插件 就是说在编译文件的时候,先把 build或dist (就是放生产环境用的文件) 目录里的文件先清除干净,再生成新的带有hash值的文件 cnpm ...
- SpringBoot 2.x (13):整合ActiveMQ
ActiveMQ5.x不多做介绍了,主要是SpringBoot的整合 特点: 1)支持来自Java,C,C ++,C#,Ruby,Perl,Python,PHP的各种跨语言客户端和协议 2)支持许多高 ...
- sublime text 快捷键新建.vue
第一步:添加模板: 模板写法如下: <template> </template> <script type="ecmascript-6"> &l ...
- cf519D. A and B and Interesting Substrings(前缀和)
题意 给出$26$个字母对应的权值和一个字符串 问满足以下条件的子串有多少 首尾字母相同 中间字母权值相加为0 Sol 我们要找到区间满足$sum[i] - sum[j] = 0$ $sum[i] = ...
- freopen()函数
freopen函数通过实现标准I/O重定向功能来访问文件,而fopen函数则通过文件I/O来访问文件. freopen函数在算法竞赛中常被使用.在算法竞赛中,参赛者的数据一般需要多次输入,而为避免重复 ...
- PostgreSQL: epoch 新纪元时间的使用
新纪元时间 Epoch 是以 1970-01-01 00:00:00 UTC 为标准的时间,将目标时间与 1970-01-01 00:00:00时间的差值以秒来计算 ,单位是秒,可以是负值; 有些应用 ...
- BZOJ 2654: tree Kruskal+二分答案
2654: tree Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1863 Solved: 736[Submit][Status][Discuss ...
- JavaScript内存泄露,闭包内存泄露如何解决
本文原链接:https://cloud.tencent.com/developer/article/1340979 JavaScript 内存泄露的4种方式及如何避免 简介 什么是内存泄露? Java ...