Question

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

Solution -- HashMap

 public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
int length = nums.length;
if (length <= 1 || k < 1)
return false;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int min = Integer.MAX_VALUE;
for (int i = 0; i < length; i++) {
if (map.containsKey(nums[i])) {
int prev = map.get(nums[i]);
int gap = i - prev;
min = Math.min(min, gap);
}
map.put(nums[i], i);
}
if (min <= k)
return true;
else
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 ...

随机推荐

  1. bzoj3393 [Usaco2009 Jan]Laserphones 激光通讯

    Description Input 第1行输入w和H,之后W行H列输入地图,图上符号意义如题目描述. Output 最少的对角镜数量. Sample Input 7 8 ....... ...... ...

  2. tyvj1297 小气的小B

    描述 其实你们都不知道,小B是很小气的.一天小B带着他的弟弟小B'一起去摘果子,走着走着,他们忽然发现了一颗长满了果子的树.由于弟弟长得太矮了,弟弟只有让哥哥小B帮他摘一些果子下来.哥哥小B说:&qu ...

  3. JSTL解析——007——fmt标签库02

    各位亲们,近期事情比较多,没更新,come on! 1.<fmt:bundle>/<fmt:message>/<fmt:param>资源国际化标签 java中使用R ...

  4. xagrs

    find /tmp/ -name "*.log" -mtime +4 | xargs -i -t mv {} /home/ find /tmp/ -name "*.log ...

  5. python之路-模块 WebDriver API

    相关文档: http://selenium-python.readthedocs.org/en/latest/api.html#selenium.common.exceptions.InvalidEl ...

  6. [Regex Expression] Confirmative -- World bundry

    String to check: As it turns out, our potential shipmates are extremely superstitious. As such, we d ...

  7. VML :Vector Markup Language

    在以前老是浏览器IE<9在不支持SVG情况下,IE一般通过VML来绘制图形,图片,文字等 步骤: 必须在头部添加 <HTML xmlns:v="urn:schemas-micro ...

  8. ASP.NET 后台下载文件方法

    void DownLoadFile(string fileName) { string filePath = Server.MapPath(fileName);//路径 //以字符流的形式下载文件 F ...

  9. 提示框的优化之自定义Toast组件之(三)Toast组件优化

    开发步骤: 在toast_customer.xml文件中添加一个图片组件对象显示提示图片 <?xml version="1.0" encoding="utf-8&q ...

  10. iOS应用拨打电话

    方法一: 特点: 直接拨打, 不弹出提示. 并且, 拨打完以后, 留在通讯录中, 不返回到原来的应用. //拨打电话 - (void)callPhone:(NSString *)phoneNumber ...