Description:

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 i and j is at most k

 

public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<nums.length; i++) { if(map.containsKey(nums[i])) {
int j = map.get(nums[i]);
if(i-j<=k) {
return true;
}
else {
map.remove(nums[j]);
map.put(nums[i], i);
}
}
else {
map.put(nums[i], i);
}
}
return false;
}
}

LeetCode——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(II,III)

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

  3. [LeetCode] 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 (判断重复元素)

    题意:如果有两个相同的元素,它们之间的距离不超过k,那么返回true,否则false. 思路:用map记录每个出现过的最近的位置,扫一边序列即可.扫到一个元素就判断它在前面什么地方出现过.本题数据有点 ...

  5. leetcode Contains Duplicate II python

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

  6. LeetCode & Q219-Contains Duplicate II

    Array Hash Table Description: Given an array of integers and an integer k, find out whether there ar ...

  7. leetcode:Contains Duplicate和Contains Duplicate II

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

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

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

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

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

随机推荐

  1. ftp的主动模式active mode和被动模式 passive mode的配置和区别

    ftp的主动模式active mode和被动模式 passive mode的配置和区别 2017年05月08日 17:47:16 阅读数:21768 本文主要记录的是在linux上的区别,弄了一整天才 ...

  2. java-结合c3p0封装的db 事务 类

    将Connection对象,绑定到当前线程中去,这样在每一个方法中都能使用这个链接. DataSourceUtils.java package com.itheima.utils; import ja ...

  3. 终于想明白一些事,关于NAS

    一直以来想搞好一部NAS存储小孩的视频和照片,一直纠结用什么硬件,硬件解决后虽然不甚满意,不过无论怎么样都算投入巨资(超过7千……)组装完毕,然后就一直纠结用什么NAS系统,终于下定决心使用了OMV, ...

  4. 关于Cocos2d-x中MoveTo等动作位置坐标和setPosition的位置坐标的区别

    setPosition设置的坐标使用的是锚点的位置,会根据锚点的改变而有所不同 而MoveTo等动作位置坐标使用的是物体中心的位置,不受锚点的影响

  5. tensorflow函数学习笔记

    https://www.w3cschool.cn/tensorflow_python/tensorflow_python-4isv2ez3.html tf.trainable_variables返回的 ...

  6. spring mvc 下载安装

    https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework ...

  7. MFC中的UpdateData()

    UpdateData()是MFC的窗口函数,用来刷新数据的,参数只有一个,默认为TRUE 简单的说: UpdateData(TRUE) == 将控件的值赋值给成员变量, UpdateData(FALS ...

  8. C# 发邮件 服务器响应为: 5.7.0 Must issue a STARTTLS command first

    The SMTP server requires a secure connection or the client was not authenticated. The server respons ...

  9. 用C语言显示汉字的演示程序

    汉字是方块字,宽高相等的汉字库在嵌入式领域有着广泛的应用,且其解析也相对来说是比较简单的.汉字在汉字库中的索引一般会遵循GB2312/GBK编码规则,GB2312/GBK规定汉字编码由2个字节组成,其 ...

  10. 网页中Span和Div的区别

    它们被用来组合一大块的HTML代码并赋予一定的信息,大部分用类属性class和标识属性id与元素联系起来,见CSS中级指南的类和id选择符. span和div的不同之处在于span是内联的,用在一小块 ...