leetcode-219-Contains Duplicate II(使用set来判断长度为k+1的闭区间中有没有重复元素)
题目描述:
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
Example 1:
Input: [1,2,3,1], k = 3
Output: true
Example 2:
Input: [1,0,1,1], k =1
Output: true
Example 3:
Input: [1,2,1], k =0
Output: false
要完成的函数:
bool containsNearbyDuplicate(vector<int>& nums, int k)
说明:
1、这道题给定一个vector和一个整数k,要求判断能不能找到两个不同位置的相同元素,他们的位置分别是i和j,使得i-j的绝对值不超过k。
2、这道题相比起上一道“找到两个重复的元素”,增加了距离k的限制。
首先,我们能够判断如果k<=0,那么必定是不存在两个不同位置的相同元素的。
其次,如果k>=nums.size()-1,那么这道题也就是上一道“找到两个重复的元素”的做法。
所以我们只需要关注k<nums.size()这种情况下,我们要如何判断。
最简单最暴力的方法当然是双重循环,设定窗口长度为k+1,从nums的第一位开始,判断窗口内有没有跟首元素相同的元素。
接着窗口不断往后挪,去掉第一个元素,增加一个新的元素,判断窗口的首元素,也就是这时候nums的第二个元素,有没有在窗口内出现重复元素。
这种做法时间复杂度O(n^2)
我们也可以仍然往后挪窗口,只不过使用set,用哈希的方法来判断窗口中有没有重复元素,这种判断比起上述暴力方法快了许多。
时间复杂度大致是O(n),因为哈希的时间时间复杂度是O(1)?
代码如下:(附详解)
bool containsNearbyDuplicate(vector<int>& nums, int k)
{
int s1=nums.size();
if(k<=0)//边界条件
return false;
if(k>=s1-1)//转化为上一道题,“找到两个重复的元素”
{
sort(nums.begin(),nums.end());
for(int i=0;i<s1-1;i++)
{
if(nums[i]==nums[i+1])
return true;
}
return false;
}
unordered_set<int>set1(nums.begin(),nums.begin()+k+1);//使用set来存储,初始化其中有k+1个元素
if(set1.size()!=k+1)//初始情况下的判断
return true;
for(int i=k+1;i<s1;i++)
{
set1.erase(nums[i-k-1]);//删去首位元素
set1.insert(nums[i]);//增加后一位新的元素,这个插入过程其实包含了判断有没有重复,决定要不要插入到set中
if(set1.size()!=k+1)//用set的size来判断
return true;
}
return false; }
上述代码实测24ms,beats 97.46% of cpp submissions。
leetcode-219-Contains Duplicate II(使用set来判断长度为k+1的闭区间中有没有重复元素)的更多相关文章
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- LeetCode 219. Contains Duplicate II (包含重复项之二)
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- LeetCode 219 Contains Duplicate II
Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...
- Java for LeetCode 219 Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i ...
- Leetcode 219 Contains Duplicate II STL
找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...
- (easy)LeetCode 219.Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i ...
- Java [Leetcode 219]Contains Duplicate II
题目描述: Given an array of integers and an integer k, find out whether there are two distinct indices i ...
- C#解leetcode 219. Contains Duplicate II
该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...
随机推荐
- Emacs中编辑保存makefile文件时会错误地将TAB转成空格的解决方法
问题描述 我的Emacs使用了Purcell的配置,在其配置中使用了whitespace-cleanup,且通过在.emacs.d/lisp/init-edit-utils.el中设定: (requi ...
- RNA -seq
RNA -seq RNA-seq目的.用处::可以帮助我们了解,各种比较条件下,所有基因的表达情况的差异. 比如:正常组织和肿瘤组织的之间的差异:检测药物治疗前后,基因表达的差异:检测发育过程中,不同 ...
- Laravel 引入自定义类库或第三方类库
强烈建议引入的类 都是含有命名空间的,这样使用起来就不会出现重名的情况.!!当然,没有命名空间也可以使用,就是类名字(非文件名)最好复杂一些.(重复也不要紧,程序会自己判断) laravel5.4中如 ...
- 如何用Python实现常见机器学习算法-2
二.逻辑回归 1.代价函数 可以将上式综合起来为: 其中: 为什么不用线性回归的代价函数表示呢?因为线性回归的代价函数可能是非凸的,对于分类问题,使用梯度下降很难得到最小值,上面的代价函数是凸函数的图 ...
- Hadoop中Comparator原理
在前面的博文<Hadoop中WritableComparable 和 comparator>中,对于WritableComparator说的不够细致,下面说说具体的实现原理! 1.Writ ...
- (一)JQuery动态加载js的三种方法
Jquery动态加载js的三种方法如下: 第一种: $.getscript("test.js"); 例如: <script type="text/javascrip ...
- Tomcat 系统架构与设计模式1
从 Tomcat 如何分发请求.如何处理多用户同时请求,还有它的多级容器是如何协调工作的角度来分析 Tomcat 的工作原理,这也是一个 Web 服务器首要解决的关键问题 Tomcat 总体结构 To ...
- Varnish 学习资料收集
高性能HTTP加速器Varnish(安装配置篇) 利用Varnish构建Cache服务器笔记 Varnish代理服务器部署 Varnish基础概念详解 Varnish的配置语言VCL及其内置变量介绍 ...
- Solr查询query效果对比
q条件 默认分词(org.apache.solr.analysis.TokenizerChain) "parsedquery" IK分词(org.wltea.analyzer.lu ...
- 使用testNGListenter来自定义日志
背景 用testNG写用例的时候,只是打印了请求的日志,没有打印这个用例的开始和结束的标识,想加上这个标识这样更好的排查问题 这种日志是加在用例开始执行和结束,相当于spring中的AOP功能,今天翻 ...