固定长度的滑动窗口+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. 如何使用jQuery给asp.net的TextBox取值和赋值

    解决办法: 可以在控件中先设置属性ClientInstandName的值和ID的值一样,再使用$("#ID").val("12345")

  2. Apsara Clouder云计算专项技能认证:网站建设-简单动态网站搭建

    一.课程介绍 1.课程目标 了解静态网站和动态网站的区别 掌握动态网站的不同实现方式 在阿里云上如何搭建 wordPress网站以及 wordPress 网站的管理和优化 二.网站搭建的类型 1.网站 ...

  3. SpringBoot 将自制的Starter 发布到远程公服

    上一篇文章:就是简单的介绍了如何自己制作一个starter ,由于上篇文章只是我个人的笔记,就是将其中重要的部分写出来了,少了其他的基础步骤,但是这个我自己就能看懂,也算不上是一篇好的博客,只能算是笔 ...

  4. OpenCV 级联分类器

    #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" ...

  5. 深入探讨Java中的异常与错误处理

    Java中的异常处理机制已经比较成熟,我们的Java程序到处充满了异常的可能,如果对这些异常不做预先的处理,那么将来程序崩溃就无从调试,很难找到异常所在的位置.本文将探讨一下Java中异常与错误的处理 ...

  6. 微信小程序开发-易源API的调用

    起因:在开发一款旅游类微信小程序时,需要接入大量的景点信息,此时可以选择自己新建数据库导入数据并读取,但是对于我来说,因为只有一个人,数据库还涉及到需要维护方面,选择调用已有API. 过程:首先查阅微 ...

  7. 第一课 安装wamp环境

    1.准备 怎样选择PHP的版本 IIS 如果想使用IIS配置PHP的话,那么需要选择Non-Thread Safe(NTS)版本的PHP Apache 如果你是用的Apache的版本来自Apache ...

  8. Ajax如何提交数据到springMVC后台

    现在好多web项目实现前段和后端分离,实现前端和后端技术人员,使他们加快开发,减少沟通上的问题,后台只需要提供访问接口,而前天只需要调用提供的接口即可.减少前后端的沟通上的成本 本项目是开发中发现aj ...

  9. 科学家用AI看月球后,却发现了这些东西

    ​​人工智能(AI)几乎已经无所不在,我们生活的大多数方面都已经被它们渗透,随着AI在过去几年取得的令人震惊的进步,它在许多方面都可能帮助我们的生活变得更美好.近日,AI在月球上发现了近7000个未被 ...

  10. 推荐使用concurrent包中的Atomic类

        这是一个真实案例,曾经惹出硕大风波,故事的起因却很简单,就是需要实现一个简单的计数器,每次取值然后加1,于是就有了下面这段代码:           private int counter = ...