【medium】220. Contains Duplicate III
因为要考虑超时问题,所以虽然简单的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的更多相关文章
- 【LeetCode】220. Contains Duplicate III
		题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ... 
- (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 ... 
- 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 ... 
- 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 ... 
- 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 ... 
- 2. Add Two Numbers【medium】
		2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ... 
- 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 ... 
- 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 ... 
- 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 ... 
随机推荐
- ES7
			本文是自己所学的ES7的一些常用的新特性: 一.padStart()方法,padEnd()方法: 如果某个字符串不够指定长度,有两个方法可以在头部或尾部补全.padStart()用于头部补全,padE ... 
- python中打印中文
			python中打印中文 在python 2.x版本中,默认是ASCII编码方式,在有业务需要输入中文时,就会出现乱码的情况.解决这种问题的一个方式就是设置py文件的编码方式.实现方式如下: 在py文件 ... 
- js中“==”与“===”区别
			直接上代码 if(2==='2'){ console.log(true) }else{ console.log(false) } //打印结果 false if(2=='2'){ console.lo ... 
- 只能用Android studio做平台移植了! 在Windows10下, 开发Android。
			安装好IDE后, 会一直显示同步失败, 看看如下步骤: 需要注意的是: -> 安装NDK 自带的NDK版本有问题 自己去下一个15版本的 -> 按照系统提示一步一步安装其他 ... 
- 从源码看springboot默认的资源文件和配置文件所在位置
			首先,使用的springboot版本是2.X,在这里写一点学习springboot的记录 springboot需要配置的不多,但也并不是完全不需要配置就可以顺畅使用,这里看一下它默认的配置 首先,看一 ... 
- python之装饰器初识
			一.@abstractmethod 1.抽象类的作用:规范编程模式 多人开发.复杂的需求.后期的扩展 是一种用来帮助我们完成规范化的手段 2.如何定义抽象类 1,from abc import ABC ... 
- element-ui和npm、webpack、vue-cli搭建Vue项目
			一.element-ui的简单使用 1.安装 1. npm 安装 推荐使用 npm 的方式安装,它能更好地和 webpack 打包工具配合使用. npm i element-ui -S 2. CDN ... 
- BZOJ 2200 道路与航线 (算竞进阶习题)
			dijkstra + 拓扑排序 这道题有负权边,但是卡了spfa,所以我们应该观察题目性质. 负权边一定是单向的,且不构成环,那么我们考虑先将正权边连上.然后dfs一次找到所有正权边构成的联通块,将他 ... 
- 利用 Python_tkinter 完成 2048 游戏
			成品展示 具备基本的数据合并以及分数统计,不同数字的色块不同 产生随机数, 数据无法合并判定以及重新开始选项 同时可以判定游戏失败条件 需求分析 完成基本数据合并算法 游戏结束条件 界面展示 重置按钮 ... 
- elasticsearch5之Elastalert 安装使用 配置邮件报警和微信报警
			简介 Elastalert是用python2写的一个报警框架(目前支持python2.6和2.7,不支持3.x),github地址为 https://github.com/Yelp/elastaler ... 
