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. HTML5学习笔记简明版 目录索引

    http://www.cnblogs.com/TomXu/archive/2011/12/06/2277499.html

  2. Shell下通过echo+telnet在远端执行命令

    创建脚本cmd.sh,用于输入telnet的用户与密码,以及生成远端需要执行的命令   执行命令 MY_SIGN=/tmp/sign; (sh cmd.sh ) | (telnet localhost ...

  3. 用户登陆,退出等基本Action

    用户登陆页面user_login.jsp对应action为login.do: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...

  4. redis 的基本语法

    Redis::__construct构造函数 $redis = new Redis(); connect, open 链接redis服务 参数 host: string,服务地址 port: int, ...

  5. All X(思维)

    All X Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Subm ...

  6. vs2010 正式版官方下载地址

    北京时间2010年4月12日12:00,微软Visual Studio 2010 正式版提供官方下载,眼下有三个版本号,Professional/Premium/Ultimate. 注意原页面的链接R ...

  7. iOS ARC注释和错误的解决方法在使用

    1.一个错误The current deployment target does not support automated __weak references 这个错误被所述支持iOS版本号不支持相 ...

  8. .NET基础拾遗(1)类型语法基础和内存管理基础1

    一.基础类型和语法 1.1 .NET中所有类型的基类是什么? 在.NET中所有的内建类型都继承自System.Object类型. 1.2 值类型和引用类型的区别? 在.NET中的类型分为值类型和引用类 ...

  9. IoC容器Autofac正篇之解析获取(五)

    解析获取的方式有如下几种: Resolve class Program { static void Main(string[] args) { var builder = new ContainerB ...

  10. js原生Ajax的封装与使用

    一.原生Ajax代码的封装如下: (function() { var XHR = { createStandardXHR: function() { return new XMLHttpRequest ...