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. Linux文件系统挂载管理

    http://itercast.com/lecture/19 文件系统创建好之后需要挂载到系统中方可使用,windows.Mac系统会自动挂载文件系统,而Linux下一般需要手工挂载或配置系统进行自动 ...

  2. Linux Java的环境变量搭建

    JAVA JDK:http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载完后,解压完并将其中的jdk文件夹移动到/u ...

  3. linux使用共享内存通信的进程同步退出问题

    两个甚至多个进程使用共享内存(shm)通信,总遇到同步问题.这里的“同步问题”不是说进程读写同步问题,这个用信号量就好了.这里的同步问题说的是同步退出问题,到底谁先退出,怎么知道对方退出了.举个例子: ...

  4. wpf纯前台绑定

    <Window x:Class="Example1.MainWindow" ... xmlns:local="clr-namespace:Example1" ...

  5. pyqt treeview基础学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import  * from Py ...

  6. NET基础课--NET的一些概念0

    0.CIL--公共中间语言 VC++:经过预编译,编译,汇编和链接生成本地可执行代码,支持它运行的是OS和本地cpu指令集. C#:    在.net框架下类似c#的高级语言经过编译生成的文件叫程序集 ...

  7. Linq GroupJoin 使用

    备忘: var data = BoshccEntities.Current.TB_MB_1 .GroupJoin(BoshccEntities.Current.TB_MB_2, o => o.H ...

  8. td 单元格 内容自动换行

    <table width="100%" border="1" align="center"> <tr> <td ...

  9. .NET AOP的实现

    一.AOP实现初步 AOP将软件系统分为两个部分:核心关注点和横切关注点.核心关注点更多的是Domain Logic,关注的是系统核心的业务:而横切关注点虽与核心的业务实现无关,但它却是一种更Comm ...

  10. Android 使用Facebook的 Stetho工具

    Stetho在Android Studio中用: 1, 引入 compile 'com.facebook.stetho:stetho:1.3.1' compile 'com.facebook.stet ...