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 ...
随机推荐
- advance shading--光源的类型
我们这里讨论的光源类型都有一个相同点,就是,我们考量的都是光源上的一个点,对于物体表面上一个点的影响,也就是说立体角趋近为零的情况. 这里光源分为两类,一类是方向光,假设光源在无限远处.另一类是点光源 ...
- sklearn中决策树算法DesiciontTreeClassifier()调用以及sklearn自带的数据包sklearn.datasets.load_iris()的应用
决策树方法的简单调用记录一下 clf=tree.DecisionTreeClassifier() dataMat=[];labelMat=[] dataPath='D:/machinelearning ...
- 解决windows搭建jenkins执行selenium无法启动浏览器问题
因为jenkins是用windows installer 安装成windows的服务了,那么jenkins是一个后台服务,所以跑selium cases 的时候不显示浏览器 Step 1. Contr ...
- oracle 11g 创建普通用户
CREATE USER xiaoming IDENTIFIED by xm123123 GRANT CREATE SESSION TO xiaoming; GRANT RESOURCE TO xiao ...
- 实验二《Java面向对象》实验报告
一.程序设计中临时变量的使用 import java.util.Arrays; public class Array { public static void main(String[] args) ...
- [转]ASP.NET Web API对OData的支持
http://www.cnblogs.com/shanyou/archive/2013/06/11/3131583.html 在SOA的世界中,最重要的一个概念就是契约(contract).在云计算的 ...
- Python + selenium + unittest装饰器 @classmethod
前言 前面讲到unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间. 于是就想是不是可以 ...
- 20145233《网络对抗》Exp9 Web安全基础实践
20145233<网络对抗>Exp9 Web安全基础实践 实验问题思考 SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符 ...
- 如何在powerdesign15.1中使用自增列
点击要设置为自增列的列 右键选择properties(或者按下ALT+enter) 点选红框,再点击Microsoft选项卡, 输入开始值和自增值即可 来自为知笔记(Wiz)
- [转载]SQL Server行列转换实现
可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P 完整语法: table_source PIVOT( 聚合函数(value_ ...