Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k.

不能动态维护k+1大小的map,因为若有相同元素,删除元素时会将新加入的相同大小的元素删掉,导致错误。

 class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
map<int,int> hmap;
int n=nums.size();
if(n<) return false;
for(int i=;i<n;i++)
{
if(hmap.count(nums[i])&&i-hmap[nums[i]]<=k)
return true;
else
hmap[nums[i]]=i; }
return false;
}
};

Contains Duplicate II的更多相关文章

  1. [leetcode] Contains Duplicate II

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

  2. leetcode:Contains Duplicate和Contains Duplicate II

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

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

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

  4. Contains Duplicate,Contains Duplicate II,Contains Duplicate III

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

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

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

  6. 217/219. Contains Duplicate /Contains Duplicate II

    原文题目: 217. Contains Duplicate 219. Contains Duplicate II 读题: 217只要找出是否有重复值, 219找出重复值,且要判断两者索引之差是否小于k ...

  7. [LeetCode] Contains Duplicate & Contains Duplicate II

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

  8. 219. Contains Duplicate II【easy】

    219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...

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

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

  10. LeetCode_219. Contains Duplicate II

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

随机推荐

  1. tableview直接滚动至最后一行的问题

    tableview直接滚动至最后一行 类似聊天界面,tableview应该直接显示在最后一行,并且不应该有滚动的出现. 在网上查了很久,直接滚动至最后一行很容易实现,有两种方法比较好. 1. 调用sc ...

  2. C语言中do...while(0)用法小结

    在linux内核代码中,经常看到do...while(0)的宏,do...while(0)有很多作用,下面举出几个: 本文地址:http://www.cnblogs.com/archimedes/p/ ...

  3. UINavigationBar 和 UINavigationItem的属性设置

    #import "RootViewController.h" @interface RootViewController () @end @implementation RootV ...

  4. MyBatis入门(四)---动态SQL

    一.创建数据库表 1.1.创建表 USE `mybatis`; /*Table structure for table `user` */ DROP TABLE IF EXISTS `user`; C ...

  5. ios appIcon配置

    iOS 我所知道的Assets.xcassets 字数923 阅读723 评论1 喜欢3 Assets.xcassets是用来存放图像资源文件的.将一个图片放在Assets里面是这个样子的 目录结构 ...

  6. 人机交互—对win10自带输入法的评价

    在我的电脑换成win10系统后我就用他自带的输入法,它可以中英文切换,用起来很方便,就用了它,就没有下载别的输入法. 用户界面:这个输入法的界面非常简单,没有像搜狗,百度之类的皮肤一说,看起来很简单, ...

  7. Oracle查看所有用户

    1.查看所有用户:select * from dba_users;   select * from all_users;   select * from user_users; 2.查看用户或角色系统 ...

  8. Android调用Web服务

    现在大部分应用程序都把业务逻辑处理,数据调用等功能封装成了服务的形式,应用程序只需要调用这些web服务就好了,在这里就不赘述web服务的优点了.本文总结如何在android中调用Web服务,通过传递基 ...

  9. hdu 2583 permutation

    permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  10. UEdit初始化加载内容偶尔失败,解决

    var ue1 = UE.getEditor('aaa',{toolbars: [],autoHeightEnabled: false}); $(function(){ var content =$( ...