因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器。

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.

|nums[i] - nums[j]| <= t

|i - j| <= k 

class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
//nums[i] and nums[j] is at most t
//i and j is at most k
/*
//两个for循环是超时的………………必须把O(n*n)变成O(n*logn)
int len = nums.size();
if (len==0 || len==1 || k<0 || t<0)
return false;
for (int i=0;i<len;i++){
for (int j=i+1;j<len && j<=i+k;j++){
if (abs((long)nums[i]-(long)nums[j])<=t)
return true;
}
}
return false;
*/
    //这个实现是能找到的写的最简单的_(¦3」∠)_
map<long, int> m;
int j = ;
for (int i = ; i < nums.size(); ++i) {
if (i - j > k && m[nums[j]] == j)
m.erase(nums[j++]);
auto a = m.lower_bound((long)nums[i] - t);
if (a != m.end() && abs(a->first - nums[i]) <= t)
return true;
m[nums[i]] = i;
}
return false;
}
};

唉…感觉一遇到难的题,就变成答案的搬运工…还是要努力呐

题目大意:给一个整数数组,找到是否存在两个不同的下标i和j,使得nums[i]和nums[j]的差的绝对值不超过t并且i和j的差的绝对值不超过k
分析

建立一个map,对应的是元素的值到元素的下标的映射。

指针i将nums数组从头遍历到尾,j指针一开始指向0。i向右走的时候如果i和j之差大于k,且m中有nums[j],就将nums[j]从m中移除,且j向前走一步。这样就保证了m中所有的元素满足第一个条件:i和j的差的绝对值不超过k

接下来考虑nums[i]和nums[j]的差的绝对值不超过t,abs(num[i] – nums[j]) <= t 则 nums[j]的最小可能满足条件的值为>=nums[i] – t的,所以使用map中的lower_bound,寻找第一个大于等于nums[i] – t的地方,找到后标记为a,此时的a只是取到了可能满足的最小的a,但(a – nums[i])不一定满足,所以检验a是否存在于map中且是否abs(a->first – nums[i]) <= t。如果都满足说明可以return true
为什么要循环的最后写map的插入呢,因为比较的是i之前的所有元素。为了防止找到的是nums[i]本身,然后让nums[i]自己本身和自己比较差值了,这样结果就不对了。
如果到最后都没有能够return true,则return false

语法上可以学习的点:

对于auto而言(C++11新特性!!),其在于type deduce,那么第一点,它不会允许没有初始化值的声明,如:

int x;

auto y; // error
aoto可以节省很多的字,比如容器的iterator:
vector <int> v;
vector <int> ::iterator iter = v.begin();  //第一种写法
auto I = v.begin();    //第二种写法
关于auto的讨论:https://www.zhihu.com/question/35517805
关于C++11的新特性:https://www.cnblogs.com/feng-sc/p/5710724.html

【medium】220. Contains Duplicate III的更多相关文章

  1. 【LeetCode】220. Contains Duplicate III

    题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  2. (medium)LeetCode 220.Contains Duplicate III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  3. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  4. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  5. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

  6. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  7. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  8. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  9. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

随机推荐

  1. 单机部署 ELK

    对于一个体量不大的系统,运行在单机上的 ELK 就足以胜任日志的处理任务了.本文介绍如何在单台服务器上安装并配置 ELK(elalasticsearch + logstash + kibana),并最 ...

  2. Grunt自动化构建环境搭建

    1.环境准备 需要安装Git.Node.Bower.Grunt.Ruby NodeJS https://nodejs.org/en/ Ruby    http://rubyinstaller.org/ ...

  3. OracleSql语句学习(三)

    --在SELECT子句中出现的函数或表达式会在结果集中作为字段名,这样的可读性差,因此可以为--这样的字段添加别名(别名中不能出现空隔,除非是用双引号括起来的)--别名中如果希望包含空隔或者区分大小写 ...

  4. Django缓存机制--rest_framework中节流源码使用的就是django提供的缓存api

    一.配置缓存   https://www.jb51.net/article/124434.htm 二.缓存全站.页面.局部   三.自我控制的简单缓存API API 接口为:django.core.c ...

  5. 基于vue现有项目的服务器端渲染SSR改造

    前面的话 不论是官网教程,还是官方DEMO,都是从0开始的服务端渲染配置.对于现有项目的服务器端渲染SSR改造,特别是基于vue cli生成的项目,没有特别提及.本文就小火柴的前端小站这个前台项目进行 ...

  6. codeforces660C

    Hard Process CodeForces - 660C You are given an array a with n elements. Each element of a is either ...

  7. kubernetes 创建用户配置文件来访问集群API

    创建一个账号 kubectl create serviceaccount def-ns-admin 绑定集群权限 kubectl create rolebinding def-ns-admin --c ...

  8. Java使用J4L识别验证码

    1.首先要下载j4l的相应文件和jar 下载地址:http://www.java4less.com/ocrtools/ocrtools.php?info=download 2.下载完成之后解压,文件目 ...

  9. 关于win10企业版在极域电子教室软件 v4.0 2015 豪华版的全屏控制下如何取得自由

    注.可能因为系统和软件的缘故无法实现 背景 由于在听课过程过于自闭,于是想自己去网上搜点东西看下 于是 经过了一番乱搞 逐渐摸索出了现方法. 方案1: 大力出奇迹 由于电脑在刚刚进入的状态的时候有段时 ...

  10. Lightning Conductor 洛谷P3515 决策单调性优化DP

    遇见的第一道决策单调性优化DP,虽然看了题解,但是新技能√,很开森. 先%FlashHu大佬,反正我是看了他的题解和精美的配图才明白的,%%%巨佬. 废话不多说,看题: 题目大意 已知一个长度为n的序 ...