【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 ...
随机推荐
- Java Lucene入门
1.lucene版本:7.2.1 pom文件: <?xml version="1.0" encoding="UTF-8"?> <project ...
- IL指令表
名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. Add.Ovf.Un 将两个无符号整数值相加,执行溢出检查,并且 ...
- react的jsx语法
在webpack.config.js中配置解析的loader { test:/\.jsx?$/, use:{ loader:"babel-loader", options:{ pr ...
- 访问docker仓库
仓库(Repositiry)是集中存放镜像的地方,分为公共仓库和私有仓库.一个容易与之混淆的概念是注册服务器(Registry).实际上注册服务器是存放仓库的具体服务器,一个注册服务器上可以有多个仓库 ...
- monkey自定义脚本实践
自定义脚本的稳定性测试 常规Monkey测试执行的是随机的事件流,但如果只是想让Monkey测试某个特定场景这时候就需要用到自定义脚本了,Monkey支持执行用户自定义脚本的测试,用户只需要按照Mon ...
- 三菱FX系列PLC教程
标 题 日 期 点击 第一章:可编程控制器概论 2014-11-04 1401 1-0 课程概述 2014-11-05 192237 1-1 PLC的定义功能与特点 2014-11-05 16 ...
- Android插件化 学习
原文:http://weishu.me/2016/01/28/understand-plugin-framework-overview/ 代码:https://github.com/tiann/und ...
- Hive 执行作业时报错 [ Diagnostics: File file:/ *** reduce.xml does not exist FileNotFoundException: File file:/ ]
2019-03-10 本篇文章旨在阐述本人在某一特定情况下遇到 Hive 执行 MapReduce 作业的问题的探索过程与解决方案.不对文章的完全.绝对正确性负责. 解决方案 Hive 的配置文件 ...
- 初步了解jQuery
jQuery 库可以通过一行简单的标记被添加到网页中. jQuery是一个JavaScript函数库. jQuery是一个轻量级的"写的少,做的多"的JavaScript库. jQ ...
- HashMap初认识
什么是HashSet? 它实现了Set接口,HashSet是Set集合的子类 有哈希表支持的,元素不可重复的哈希码值(实际上是一个HashMap的实例). 它不保证set的迭代顺序(遍历元素的顺序), ...