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.

Subscribe to see which companies asked this question

bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int, int> hash;
int i = ;
for (auto num : nums)
{
unordered_map<int, int>::iterator iter = hash.find(num);
if (iter == hash.end())
hash.insert(make_pair(num, i));
else {
if (i - (*iter).second <= k)
return true;
(*iter).second = i;
}
++i;
}
return false;
}

Contains Duplicate II leetcode的更多相关文章

  1. 219. Contains Duplicate II - LeetCode

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

  2. Contains Duplicate II ——LeetCode

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

  3. [leetcode] Contains Duplicate II

    Contains Duplicate II Given an array of integers and an integer k, find out whether there there are ...

  4. leetcode:Contains Duplicate和Contains Duplicate II

    一.Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your fun ...

  5. 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II

     217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...

  6. LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II

     1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...

  7. [LeetCode] Contains Duplicate & Contains Duplicate II

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

  8. [LeetCode] Contains Duplicate(II,III)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

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

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

随机推荐

  1. Spring JdbcTemplate用法整理

    Spring JdbcTemplate用法整理: xml: <?xml version="1.0" encoding="UTF-8"?> <b ...

  2. Flex移动应用程序开发的技巧和窍门(四)

    范例文件 flex-mobile-dev-tips-tricks-pt4.zip 这是本系列文章的第四部分,该系列文章涵盖Flex移动开发的秘诀与窍门. 第一部分关注切换视图以及切换执行应用时的数据处 ...

  3. 统计SQLSERVER表行数,以及每天数据变化的行数

    此sql对监控系统很有帮助,知道哪些表压力大,每天的数量级大概多少等信息. 得到这些信息就可以做相应的策略来进行系统优化. create table tmp( name varchar(50), ro ...

  4. HDU4496(并查集)

    D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  5. 02月刊(上) | 微信小程序

    * { margin: 0; padding: 0 } .con { width: 802px; margin: 0 auto; text-align: center; position: inher ...

  6. java打包jar,war,ear包的作用、区别

    java的打包jar,war,ear包的作用,区别,打包方式. a) 作用与区别      i.    jar: 通常是开发时要引用通用(JAVA)类,打成包便于存放管理      ii.   war ...

  7. Swift 网络请求数据与解析

    一: Swift 网络数据请求与处理最常用第三方 又有时间出来装天才了,还是在学swift,从中又发现一些问题,这两天上网找博客看问题弄的真的心都累.博客一篇写出来,好多就直接照抄,就没有实质性的把问 ...

  8. java GUI简单记事本

    代码: import java.awt.*; import java.awt.event.*; import java.io.*; class MyMenu { //定义该窗体所需的组件,方便其他函数 ...

  9. JavaSE学习笔记

    1.数据类型 boolean char byte short int long double float double array class interface 总结: 前9种基本类型,后3中引用类 ...

  10. WebSockets介绍

    Web sockets定义为在servers和clients之间的双向连接.意味着servers和clients可以同时交流并发送数据.这种协议是从底层就是双工连接.Web sockets技术上得到了 ...