题目描述:

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的闭区间中有没有重复元素)的更多相关文章

  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 ...

  2. [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)

    每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...

  3. 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 ...

  4. LeetCode 219 Contains Duplicate II

    Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...

  5. 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 ...

  6. Leetcode 219 Contains Duplicate II STL

    找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...

  7. (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 ...

  8. Java [Leetcode 219]Contains Duplicate II

    题目描述: Given an array of integers and an integer k, find out whether there are two distinct indices i ...

  9. C#解leetcode 219. Contains Duplicate II

    该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...

随机推荐

  1. 把二叉搜索树转化成更大的树 · Convert BST to Greater Tree

    [抄题]: 给定二叉搜索树(BST),将其转换为更大的树,使原始BST上每个节点的值都更改为在原始树中大于等于该节点值的节点值之和(包括该节点). Given a binary search Tree ...

  2. Golang基本结构之练习(day2)

    笔记: . 任何一个代码文件隶属于一个包 . import 关键字,引用其他包: import(“fmt”) import(“os”) 通常习惯写成: import ( “fmt” “os” ) . ...

  3. SDL编程

    一.简介 SDL是一个用C编写的跨平台的多媒体库,它通过OpenGL和Direct3D,提供了针对音频.视频.键盘.鼠标.控制杆及3D硬件的低级别的访问接口.它在MPEG播放软件.模拟器以及许多游戏中 ...

  4. java 框架收藏

    一.java 异步非阻塞编程框架 1.Spring Webflux 2.Vert.x 3.Ratpack 4.smart-socket 国产异步框架 二.微服务框架 1.Jboot :专为大型分布式项 ...

  5. boost之内存管理

    内存管理一直是令C++程序员最头疼的工作,C++继承了C那高效而又灵活的指针,使用起来稍微不小心就会导致内存泄露.野指针.越界访问等访问.虽然C++标准提供了只能指针std::auto_ptr,但是并 ...

  6. 安装bcmath 扩展

    1.在php源码包中,默认就包含bcmath扩展的安装文件,只需手动安装一下即可 cd /root/build2/php-/ext/bcmath // 进入PHP的源码包目录中的bcmatch扩展目录 ...

  7. 解决root@localhost's password:localhost:permission denied,please try again

    背景:在装完Hadoop及jdk之后,在执行start-all.sh的时候出现 root@localhost's password:localhost:permission denied,please ...

  8. Weblogic的中的文件上传

    在weblogic中在jsp页面中this.getServletContext().getRealPath("/upload")这样的写法是要报错的在jsp页面总你甚至不能使用th ...

  9. [Android] 对ImageView设置属性scaleType为FIT_START,如何去掉多余空白

    当对ImageView设置了属性scaleType为FIT_START时,可以通过调用ImageView的setAdjustViewBounds(true). 即: imageView.setScal ...

  10. Redis通用命令(七)

    Keys的通用操作: (1)获得所有的keyskeys *(2)查看my开头的keykeys my?(3)删除keydel key1 key2(4)key是否存在exists key1 exists ...