220. 存在重复元素 III
题目:
给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ。
示例 1:
输入: nums = [1,2,3,1], k = 3, t = 0
输出: true
示例 2:
输入: nums = [1,0,1,1], k = 1, t = 2
输出: true
示例 3:
输入: nums = [1,5,9,1,5,9], k = 2, t = 3
输出: false
解答:
首先这题用例挺TM蠢的,题目说了t和k为绝对值,居然还有k为负数的用例,劳资又不是ACM选手,只是想刷个算法题而已。。
1.桶排序:
照着官方题解写的。不过有几个用例过不去,如t==0的特殊情况,利用len(list)!=len(set(list))可判断,还有t<0、k<0的(sb)用例额外判断一下,以及某些极其分散的用例,如[200000000000,-2222222222222222],2,2这样的。我是判断一下llog(l)和max-min的大小关系(l是数组长度,max、min分别为数组最大最小数),若llogl小,那就直接排序解决,不然用桶排序的话要生成的桶太多了,而且几乎全部是无用的桶。
复杂度O(n)
代码:
from math import log
class Solution:
def containsNearbyAlmostDuplicate(self, nums, k, t) :
l=len(nums)
if l< or t< or k<:
return False
if (t==):
return len(nums)!=len(set(nums))
_max=max(nums)
_min=min(nums)
if l*log(l)<_max-_min:
for i in range(l):
for j in range(max(i-k,),i):
if abs(nums[i]-nums[j])<=t:
return True
return False
hashtable=[[]for i in range((_max-_min)//t+1)]
for i in range(l):
x=nums[i]
m=(x-_min)//t
for p in hashtable[m]:
if abs(p[]-i)<=k and abs(p[]-x)<=t:
return True
le,ri=m-,m+
if le>=:
for y in hashtable[le]:
if abs(y[]-i)<=k and abs(y[]-x)<=t:
return True
if ri<len(hashtable):
for z in hashtable[ri]:
if abs(z[]-i)<=k and abs(z[]-x)<=t:
return True
hashtable[m].append((i,x))
return False
2.二叉搜索树+滑动窗口:
由于对于两个数的索引差有绝对值<=k的限制,所以考虑维护一个长度k的窗口进行滑动。但由于窗口内的元素是无序的,如果对于每个窗口我们都进行排序,复杂度会太高。所以考虑用有序集合(C++里的set或者multi_set),窗口大小k由我们自己维护,窗口内的有序性交给set进行维护。每当我们滑动窗口时,考察右边移入窗口的新数字nums[i],因为窗口内的数字和当前索引i的索引差的绝对值一定满足<=k的要求,那么如果窗口内有相同的数字就可以返回true。如果没有,查找窗口内有无满足大小介于[nums[i]-t,nums[i]+t]范围内的数字,如果有则返回true。这里利用C++的lower_bound和upper_bound函数。
map/set.lower_bound(num),返回大于等于num的第一个迭代器
map/set.upper_bound(num),返回大于num的第一个迭代器
带模板的lower_bound格式是这样的:lower_bound<容器迭代器类型,容器元素类型>(起始迭代器,尾迭代器,元素),如:
lower_bound<vector<int>::iterator,int>(p.begin(),p.end(),);
upper_bound模板函数类似。
这个方法还是挺巧妙的,以前没有见过,记录下,滑动窗口不一定专属于线性结构。
复杂度:O(n logk)
代码:
比较坑爹的是有几个大数用例,得加long,不然AC不了。
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if(t< or k<){return false;}
set<long> st;
for (int i = ; i < nums.size(); ++i) {
if (st.count(nums[i])) {
return true;
}
auto it = st.upper_bound(nums[i]);
if (it != st.end() and *it - nums[i] <= t) {
return true;
}
it = st.lower_bound(long(nums[i]) - t);
if (it != st.end() and *it<nums[i]) {
return true;
}
st.insert(nums[i]);
if (st.size() == k+) {
st.erase(nums[i-k]);
}
}
return false;
}
};
2020-02-17 15:30:19
220. 存在重复元素 III的更多相关文章
- Java实现 LeetCode 220 存在重复元素 III(三)
220. 存在重复元素 III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最 ...
- Leetcode 220.存在重复元素III
存在重复元素III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ. ...
- [LeetCode]220. 存在重复元素 III
题目链接:https://leetcode-cn.com/problems/contains-duplicate-iii/ 题目描述: 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 ...
- 【每日算法】存在重复元素 III
题目描述 这是 LeetCode 上的 220. 存在重复元素 III, 难度为 [中等] 给你一个整数数组 nums 和两个整数 k 和 t .请你判断是否存在 两个不同下标 i 和 j,使得 ab ...
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [Swift]LeetCode220. 存在重复元素 III | Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- LeetCode220 存在重复元素 III
给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ. 示例 1: 输入: ...
- Leetcode 存在重复元素 (219,220)
219. 存在重复元素 II 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. / ...
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
随机推荐
- 数据结构(集合)学习之Map(二)
集合 框架关系图 补充:HashTable父类是Dictionary,不是AbstractMap. 一:HashMap中的链循环: 一般来说HashMap中的链循环会发生在多线程操作时(虽然HashM ...
- 【剑指Offer】63、二叉搜索树的第k个结点
题目描述 给定一棵二叉搜索树,请找出其中的第k小的结点.例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. 题解一:DFS 借助栈实现 // 中序非递归 public ...
- 论文阅读笔记(十六)【AAAI2018】:Region-Based Quality Estimation Network for Large-Scale Person Re-Identification
Introduction (1)Motivation: 当前的行人重识别方法都只能在标准的数据集上取得好的效果,但当行人被遮挡或者肢体移动时,往往效果不佳. (2)Contribution: ① 提出 ...
- phyon tensorflow 安装(centos)
1.phyon27 安装 1.1 下载 wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz 1.2 解压 tar -zxv ...
- SpringBoot2.x-笔记(01)
程序入口 @SpringBootApplication public class SpringbootApplication { public static void main(String[] ar ...
- 小白的java学习之路 “ 数组”
数组 一.什么是数组: 数组是一个变量,存储相同数据类型的一组数据 声明一个变量就是在内存空间划出一块合适的空间 声明一个数组就是在内存空间划出一串连续的空间 二.数组基本要素: 标识符:数组的名称, ...
- java线程池之synchronized锁
//Object 定义了一个引用类型的对象用于加锁 static Object Lock = new Object(); //定义一个int类型变量0做初始值 static int iCheck = ...
- Eigen库学习---Map类
Eigen中定义了一系列的vector和matrix,相比copy数据,更一般的方式是复用数据的内存,将它们转变为Eigen类型.Map类很好地实现了这个功能. Map定义 Map(PointerAr ...
- C# ASCII码的转换、转义字符、对照表
var splitStr = new byte[] { 0x05, 0x0D, 0x0A };//var splitStr = new byte[] { 5, 13, 10 };这样写也可以 var ...
- [POI2015]PUS [线段树优化建图]
problem 线段树优化建图,拓扑,没了. #include <bits/stdc++.h> #define ls(x) ch[x][0] #define rs(x) ch[x][1] ...