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. java 数据设置和显示

    1. 首先设置ModelAndView 的值 @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServ ...

  2. delphi edit编辑框使用

    Delphi编辑框Edit的用法 http://wenku.baidu.com/link?url=oKKm0VFBXexqiWt9ZNB8WxFGzwjJqRBM3ohrAy6GSMmOmwIzSWP ...

  3. HDU-1233-还是畅通工程(并查集)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1233题目很简单(最小生成树) #include<cstdio> #include<io ...

  4. VisualGDB Makefiles

    以下内容是VisualGDB官网对VisualGDB编译时获取相关编译信息的说明: When you create a new project using VisualGDB, it will gen ...

  5. 一篇完整的FlexBox布局指南

    一篇完整的FlexBox布局指南 转载请标注本文链接并附带以下信息: 译:Cydiacen 作者:CHRIS COYIER 原文:A Complete Guide to Flexbox 原文更新于 2 ...

  6. android:在ViewPager中使用Button

    最近在项目用用到ViewPager ,其中页面包含有Button,因为之前也有使用个ViewPager ,所以这个也照搬之前的方式,测试后发现点击button无法执行,这个button是在第一页面的默 ...

  7. POJ1753 搜索

    Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on ...

  8. TypeScript教程3

    1.快速回顾一下这JavaScript中的命名函数和匿名函数: 纯文本查看 复制代码 1 2 3 4 5 //Named functionfunction add(x, y) {     return ...

  9. C语言函数名与函数指针详解

    一.通常的函数调用 一个通常的函数调用的例子: /* 自行包含头文件 */ void MyFun(int x); /* 此处的声明也可写成:void MyFun(int) */ int main(in ...

  10. Eclipse 报java.lang.UnsupportedClassVersionError: ("yourclass") bad major version at offset=6

    报这个错误是指你的jar包或者class 的被编译的jdk版本比当前runtime的jdk版本高. 解决问题 1)如果是jar包,重新用jdk 1.6编译你的jar 包 2)如果是java文件或者项目 ...