题目

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; }
};

GitHub测试程序源码

LeetCode(220) Contains Duplicate III的更多相关文章

  1. LeetCode(219) Contains Duplicate II

    题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...

  2. LeetCode(217)Contains Duplicate

    题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  3. LeetCode(260) Single Number III

    题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...

  4. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  5. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  6. 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 ...

  7. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. IIS 在 Windows 上托管 .NET Core2.0

    使用 IIS 在 Windows 上托管 ASP.NET Core2.0 https://www.cnblogs.com/sundar/p/9195550.html 阅读目录 准备: 第一步:新建项目 ...

  2. [原]Maven项目编译后classes文件中没有.xml问题

    在做spring+mybatiss时,自动扫描都配置正确了,却在运行时出现了如下错误.后来查看target/classes/.../dao/文件夹下,发现只有mapper的class文件,而没有xml ...

  3. return void ajax

    public class UserInfo { private String name; private Integer age; public String getName() { return n ...

  4. [转]兼容各个浏览器的H.264播放: H.264+HTML5+FLOWPLAYER+WOWZA+RMTP

    一.方案确定 计划做视频播放,要求能够播放H264编码的mp4文件,各个浏览器,各种终端都能播放. 首先查找可行性方案, http://www.cnblogs.com/sink_cup/archive ...

  5. java es 骤合操作

    ElasticSearch java API - 聚合查询 以球员信息为例,player索引的player type包含5个字段,姓名,年龄,薪水,球队,场上位置.index的mapping为: &q ...

  6. 1、http简介

    HTTP 简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传 ...

  7. android dialog style属性设置

    <!--最近做项目,用到alertDialog,用系统自带的style很难看,所以查了资料自己定义了个style. res/value/style.xml内增加以下代码:--> <s ...

  8. 在Office 365 添加就地保留用户邮箱

    基于客户需求,要求将用户批量添加到Office 365中的现有就地保留.如您所了解的,我们可以通过Exchange在线图形用户GUI界面完成,也可以通过PowerShell完成. 要将用户批量添加到O ...

  9. 远程连接 mySql数据库

    远程连接 mySql数据库 一.安装并配置MySQL1.安装MySQL:运行mysql-essential-6.0.11-alpha-win32,按“MySQL+6.0+Windows下安装图解”完成 ...

  10. Unity3d 中键值监听方法

    unity3d的api中没有负责监听键值的方法,不过unity的input类是通过c#类获取各类监听事件,所以我们可以通过c#类监听,方法如下: void OnGUI() { Event e = Ev ...