固定长度的滑动窗口+set

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k)
    {
        unordered_set<int> record;

        ;i<nums.size();i++)
        {
           if(record.find(nums[i])!=record.end())
           {
               return true;
           }

           record.insert(nums[i]);

           )
           {
               record.erase(nums[i-k]);
           }
        }
        return false;

    }
};

注意这里的set是不会有重复元素出现的,所以才会有if(record.size()==k+1)这一条件的判断。

leetcode 219的更多相关文章

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

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

  3. Java实现 LeetCode 219 存在重复元素 II(二)

    219. 存在重复元素 II 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. 示 ...

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

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

随机推荐

  1. day31-hmac模块检测客户端是否合法

    #如果客户端知道服务端的ip地址和端口,就可以连接服务端,信息不安全. #使用os.urandam随机生成32位bytes,然后hmac加密之后再发送给客户端. #server: import soc ...

  2. 信息检索盛会 微软“领衔主演”——记ACM SIGIR 2013信息检索国际会议

    微软"领衔主演"--记ACM SIGIR 2013信息检索国际会议" title="信息检索盛会 微软"领衔主演"--记ACM SIGIR  ...

  3. Eclipse 热部署方式

    1.tomcat 热部署 1.1方法一:更改 server.xml,更改为 <Context docBase="dreamlive" path="/ROOT&quo ...

  4. golang seelog使用

    golang中自带的有log包,但是功能并不能满足我们.很多人推荐seelog,我们今天一起学习下. 安装 go get github.com/cihub/seelog 快速开始 引用seelog w ...

  5. 基于Docker 部署Jmeter + Grafana + InfluxDB 性能测试监控配置(亲测可用)

    工具介绍: InfluxDB:是一款用Go语言编写的开源分布式时序.事件和指标数据库,无需外部依赖.该数据库现在主要用于存储涉及大量的时间戳数据,如DevOps监控数据,APP metrics, lo ...

  6. <JZOJ1329>旅行

    贪心大水题 #include<cstdio> #include<iostream> #include<cstring> #include<algorithm& ...

  7. C# 输出&输入&类型强制转换

    输入字符串 String s; s=Console.ReadLine(); 输出字符串 Console.WritrLine(s); 输出分两种 ①占位符输出:Console.WriteLine(&qu ...

  8. SWUST OJ 东6宿舍灵异事件(0322)

    东6宿舍灵异事件(0322) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 88 Accepted: 31   Descriptio ...

  9. github 下载部分代码

    作者:知乎用户链接:https://www.zhihu.com/question/25369412/answer/96174755来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  10. 混乱中的ICO平台,会不会是下一个P2P的重灾区?

    当众多巨头和创业者还在为共享打车.共享单车.VR.IP化.互联网金融沉迷时,一种全新的"众筹"正在造就一个又一个暴富神话.其名为ICO,即首次代币众筹,一般指区块链初创项目在众筹平 ...