要求

  • 给出整型数组nums和整数k,是否存在索引i和j,nums[i]==nums[j],且i和j之间的差不超过k

思路

  • 暴力解法(n2)
  • 建立最长为k+1的滑动窗口,用set查找窗口中是否有重复元素,若没有则向后滑动(时间n,空间k)
  • 并不存在滑动窗口的实体,通过维护set中的元素实现
  • 代入具体例子确定边界

 1 class Solution {
2 public:
3 bool containsNearbyDuplicate(vector<int>& nums, int k) {
4
5 unordered_set<int> record;
6 for( int i ; i < nums.size() ; i ++ ){
7 if( record.find(nums[i]) != record.end() )
8 return true;
9 record.insert( nums[i] );
10
11 // 保持record中最多有k个元素
12 if( record.size() == k+1 )
13 record.erase( nums[i-k] );
14 }
15 return false;
16 }
17 };

相关

  • 217 Contains Duplicate

[刷题] 219 Contains Duplicate II的更多相关文章

  1. 219. Contains Duplicate II【easy】

    219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...

  2. 219. Contains Duplicate II - LeetCode

    Question 219. Contains Duplicate II Solution 题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false 思路:用一个map记录每 ...

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

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

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

  5. 219. Contains Duplicate II

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

  6. C#解leetcode 219. Contains Duplicate II

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

  7. 【LeetCode】219. Contains Duplicate II

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

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

  9. 【leetcode刷题笔记】Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

随机推荐

  1. [状压DP]子矩阵

    子 矩 阵 子矩阵 子矩阵 题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵 如,下面左图中选取第 2 . 4 ...

  2. [BFS]P1434 [SHOI2002]滑雪

    P1434 [SHOI2002]滑雪 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者 ...

  3. RE.从单链表开始的数据结构生活(bushi

    单链表 单链表中节点的定义 typedef struct LNode{ int data;//数据域 struct LNode *next;//定义一个同类型的指针,指向该节点的后继节点 }LNode ...

  4. 解决Spring中使用Example无法查询到Mongodb中的数据问题

    1 问题描述 在Spring Boot中使用Mongodb中的Example查询数据时查询不到,示例代码如下: ExampleMatcher matcher = ExampleMatcher.matc ...

  5. Circular Sequence UVA - 1584

    ​ Some DNA sequences exist in circular forms as in the following figure, which shows a circular sequ ...

  6. 谈谈react hooks的优缺点

    前言Hook 是 React 16.8 的新增特性.它是完全可选的,并且100%向后兼容.它可以让你使用函数组件的方式,运用类组件以及 react 其他的一些特性,比如管理状态.生命周期钩子等.从概念 ...

  7. 从苏宁电器到卡巴斯基第24篇:难忘的三年硕士时光 II

    没办法,还是先打好基础吧 其实在我知道自己面试失败后,第一个想法就是将面试官问我的问题都总结出来,然后通过查权威的资料,找出所有问题的答案,背下所有的答案,大概过一到两个月吧,再面试金山.当时我是这么 ...

  8. hdu4662 简单搜索打表

    题意:      给你一个初始串"MI",这个串有三种操作, (1)M后卖弄可以直接复制 ,MI -> MII (2)三个III可以变成一个U,MUIII -> MUU ...

  9. DVWA之Brute Force

    DVWA简介 DVWA(Damn Vulnerable Web Application)是一个用来进行安全脆弱性鉴定的PHP/MySQL Web应用,旨在为安全专业人员测试自己的专业技能和工具提供合法 ...

  10. hdu4975 行列和构造矩阵(dp判断唯一性)

    题意:       和hdu4888一样,只不过是数据加强了,就是给你行列的和,让你构造一个矩阵,然后判断矩阵是否唯一. 思路:       构造矩阵很简单,跑一次最大流就行了,关键是判断矩阵的唯一性 ...