题目

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. 【手撸一个ORM】第六步、对象表达式解析和Select表达式解析

    说明 一个Orm自然不仅仅包含条件表达式,还会有如下的场景: OrderBy(s => s.StudentName) Select<StudentDto>(s => new S ...

  2. Net Core下通过Proxy 模式

    Net Core下通过Proxy 模式 NET Core下的WCF客户端也是开源的,这次发布.NET Core 2.0,同时也发布了 WCF for .NET Core 2.0.0, 本文介绍在.NE ...

  3. idea报错:Error running $classname: Command line is too long. Shorten command line for $classname.

    Command line is too long 打印的变量太长了,超过了限制,这都会报错...我只想知道idea基于什么原理会报这个错... 解决 1.按照提示修改该类的配置,选择jar manif ...

  4. 第二十章 排查和调试Web程序 之 设计异常处理策略

    1. 概述 本章内容包括: 多层架构中的异常处理.使用global.asax 或 自定义的HttpHandler 或 web.config中的属性来显示特定的错误页.处理 first chance 异 ...

  5. 面向对象程序设计第四单元总结(UML系列)

    2019面向对象程序设计第四单元总结 前言 ​ 本单元是面向对象程序设计课程的最后一个单元了,本单元是和UML模型相关,也就是说,我们需要正确理解UML模型的基础上,对构建出的UML模型进行解析,但是 ...

  6. Angular 路由route实例

    iSun Design & Code AngularJS - 路由 routing 基础示例 AngularJS 路由 routing 能够从页面的一个视图跳转到另外一个视图,对单页面应用来讲 ...

  7. js 数组array es5-es6+ 新增方法函数

    arr.forEach(function(item,index,arr){},this)  相当于普通的for循环,第一个回调参数,第二个this可以重定向[箭头函数则不生效] arr.map() 非 ...

  8. ftp错误

    ftp 550 检查是否目录,文件确定存在. 服务器列表是要设置unix列表模式.

  9. Beginning Python Chapter 1 Notes

    James Payne(American)编写的<Beginning Python>中文译作<Python入门经典>,堪称是Python的经典著作. 当然安装Python是很简 ...

  10. COGS 2794. 爱摔跤的比利海灵顿

    ★☆   输入文件:find_k.in   输出文件:find_k.out   简单对比时间限制:1.4 s   内存限制:128 MB [题目描述] B•海灵顿•雷想要和n个巨人比试摔♂跤,他想先和 ...