【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 ...
随机推荐
- Entity Framework Core系列之DbContext(删除)
上一篇我们介绍了Entity Framework Core系列之DbContext(修改),这一篇我们介绍下删除数据 修改实体的方法取决于context是否正在跟踪需要删除的实体. 下面的示例中con ...
- mybatis的where和if标签配合使用
where标签用于简化sql的书写,if标签用于判断.大概的使用如下 <select id="getCountByPageInfo" parameterType=" ...
- NIO原理及案例使用
什么是NIO Java提供了一个叫作NIO(New I/O)的第二个I/O系统,NIO提供了与标准I/O API不同的I/O处理方式.它是Java用来替代传统I/O API(自Java 1.4以来). ...
- 如何去掉wordpress网站url里面的index.php(Apache服务器)
在wordpress根目录新建.htaccess文件,并拷贝以下代码保存即可. <IfModule mod_rewrite.c> RewriteEngine On RewriteBase ...
- vue生命週期
https://www.cnblogs.com/fly_dragon/p/6220273.html https://www.cnblogs.com/fly_dragon/p/6220273.html
- 7.docker日志收集
默认情况下,docker logs或者docker service logs显示命令的输出,就像在终端中以交互方式运行命令时一样.UNIX和Linux命令通常开在运行时间上三个I / O流,所谓的 S ...
- Help Me Escape ZOJ - 3640
Background If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth ...
- 用django2.1开发公司官网(上)
1.在MySQL中新建数据库 show databases;//查看已经有的数据库 create database guanwang; 2.新建django项目guan 1.使用pycharm新建dj ...
- swiper常见问题
swiper是一个比较不错的一个轮播插件,但是呢,有时候在使用的时候也会出现很多的问题,我将我遇到的一些问题解决办法写在下面. 第一个问题:swiper分页器不显示 一般swiper使用分页器都是这样 ...
- 深入浅出mybatis之返回主键ID
目录 添加单一记录时返回主键ID 在映射器中配置获取记录主键值 获取新添加记录主键字段值 添加批量记录时返回主键ID 获取主键ID实现原理 添加记录后获取主键ID,这是一个很常见的需求,特别是在一次前 ...