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 difference between i and jis at most k.

解题思路:

用一个表,记录扫描过程中,每个数字出现的最近

一次位置,则当前位置和最近位置的差小于k时 返回true,

bool containsNearbyDuplicate(vector<int>& nums, int k) {int size=nums.size();if(size<=1||k<=0)return false;unordered_map<int,int> table;for(int i=0;i<size;++i){auto it=table.find(nums[i]);if(it==table.end())table[nums[i]]=i;else{if(i-table[nums[i]]<=k)return true;it->second=i;}}return false;}

转载于:https://blog.51cto.com/searchcoding/1751329

219. Contains Duplicate II相关推荐

  1. Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]

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

  2. leetcode python3 简单题219. Contains Duplicate II

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百一十九题 (1)题目 英文: Given an array of intege ...

  3. Contains Duplicate II

    Given an array of integers and an integer k, return true if and only if there are two distinct indic ...

  4. LeetCode Contains Duplicate II(hash)

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  5. C#LeetCode刷题之#219-存在重复元素 II​​​​​​​(Contains Duplicate II)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3704 访问. 给定一个整数数组和一个整数 k,判断数组中是否存在 ...

  6. Contains Duplicate II - LeetCode

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  7. Python JAVA Solutions for Leetcode

    Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...

  8. Leetcode重点250题

    LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...

  9. LeetCode 简单算法题

    使用Nodejs 抓取的LeetCode 简单算法题  一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...

最新文章

  1. angular ngoninit 刷新html页面_web前端入门到实战:实现html页面自动刷新
  2. wp7开发第一课:软件生命周期(其一)
  3. Python学习笔记 之 变量进阶
  4. Swift设计模式: 观察者模式(发布-订阅模式)
  5. SONY图像传感器列表
  6. 解决Steam绑定手机令牌的问题
  7. 关于嵌入式的bin、hex、axf、map
  8. HTTP Live Streaming (HLS) - 概念
  9. python爬虫新浪微博评论、评论人信息
  10. 太极图(HTML+CSS),无动态效果
  11. 视频处理指定颜色的提取
  12. [HITICS]大作业——程序人生Hello‘s P2P
  13. 第二十一章 : 文本处理
  14. 基于深度神经网络的高光谱影响分类方法研究---MNF+自动编码器+Softmax (准确率比较低,17年的论文)
  15. java通过输入的方式给int数组赋值并输出数组
  16. 解决zeal文档下载不上的办法
  17. 别用Docker了,这7大开源容器化工具也能满足你!
  18. InfiniBand技术和协议架构分析
  19. Android 加密算法
  20. 泛型 super和extend

热门文章

  1. 全球顶级设计师云集天猫双11 超1000款时尚大牌新品首发
  2. 优云,新一代运维PaaS平台
  3. VMware将世界领先的云访问安全代理合作伙伴纳入移动安全联盟
  4. sql server insert values 多值 与oracle 的不同
  5. 关于github failed to push some refs
  6. 七彩岁月【我与51CTO一“七”成长】
  7. android 线程 wait notify
  8. 半年没活动了,来活动一下
  9. 开发购物车应用程序(1)
  10. 『计算机视觉』经典RCNN_其一:从RCNN到Faster-RCNN