要求

  • 给出整型数组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. 图像Resize方式对深度学习模型效果的影响

    在基于卷积神经网络的应用过程中,图像Resize是必不可少的一个步骤.通常原始图像尺寸比较大,比如常见监控摄像机出来的是1080P高清或者720P准高清画面,而网络模型输入一般没有这么大,像Yolo系 ...

  2. 翻译:《实用的Python编程》08_00_Overview

    目录 | 上一节 (7 高级主题) | 下一节 (9 包) 8. 测试和调试 本节介绍与测试.日志和调试有关的基本主题. 8.1 测试 8.2 日志,错误处理和诊断 8.3 调试 目录 | 上一节 ( ...

  3. 2021 小白版,360 行行行转 IT

    hey guys ,我是 cxuan,这一篇文章我就要和你聊聊编程如何学习,这一篇文章涉及的内容简直太多了,我将从入门开始,一步一步到如何提高,然后到一些学习的相关问题,还有一些计算机相关的术语等,干 ...

  4. Kafka优雅应用

    Kafka API实战 注意版本问题这个,kafka-client要和kafka的版本一致 <dependency> <groupId>org.apache.kafka< ...

  5. Leedcode算法专题训练(双指针)

    算法思想 双指针 167. 两数之和 II - 输入有序数组 双指针的典型用法 如果两个指针指向元素的和 sum == target,那么得到要求的结果: 如果 sum > target,移动较 ...

  6. [开源]制作docker镜像不依赖linux和Docker环境

    背景 最近群友们经常反馈docker镜像制作起来有点麻烦,我开源的antdeploy工具虽然可以制作镜像但是必须有一个提前:有一台安装好docker的linux服务器.因为大家开发环境基本上都是win ...

  7. 前端框架之争丨除了Vue、Angular和React还有谁与之争锋

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文参考:https://www.sitepoint.com/most-popular-frontend-f ...

  8. java多种文件复制方式以及效率比较

    1.背景 java复制文件的方式其实有很多种,可以分为 传统的字节流读写复制FileInputStream,FileOutputStream,BufferedInputStream,BufferedO ...

  9. Day02_15_方法重载

    方法重载 1.什么是方法重载? * 方法重载又被称为 OverLoad,是指在同一个类中,具有相同方法名的不同方法,各个方法虽然方法名相同,但是各自的形式参数不同. 2.什么时候考虑使用方法重载? * ...

  10. JUC包的线程池详解

    为什么要使用线程池 创建/销毁线程需要消耗系统资源,线程池可以复用已创建的线程. 控制并发的数量.并发数量过多,可能会导致资源消耗过多,从而造成服务器崩溃.(主要原因) 可以对线程做统一管理. JUC ...