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 absolute difference between i and j is at most k.


题目标签:Array, Hash Table

  题目给了我们一个nums array 和一个 k, 让我们找到两个 相同的项,并且它们的index 差值 小于等于 k。

  这次我们依然要利用Hash Table,因为这次需要比较它们的index, 所以需要key 和value, 不同于 包含重复项之一 那一题,只需要比较数字。

  遍历nums array, 如果nums[i] 不在hash table 里的话,就把 nums[i] 当作key,  i 当作 value 存入hash table;

           如果hash table 有nums[i]的话,就比较两个index 的差值,如果小于等于 k, 就返回true。

Java Solution:

Runtime beats 52.19%

完成日期:05/27/2017

关键词:Array, Hash Table

关键点:利用Hash Table, 把nums[i] 当作key, i 当作value 存入Hash Table

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

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 219. Contains Duplicate II (包含重复项之二)的更多相关文章

  1. [LeetCode] 219. Contains Duplicate II 包含重复元素 II

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

  2. [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)

    每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...

  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 217. Contains Duplicate (包含重复项)

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  5. [LeetCode] 220. Contains Duplicate III 包含重复元素 III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  6. C#解leetcode 219. Contains Duplicate II

    该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...

  7. 219 Contains Duplicate II 存在重复 II

    给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使 nums [i] = nums [j],并且 i 和 j 的绝对差值最大为 k. 详见:https://leetcod ...

  8. LeetCode 219 Contains Duplicate II

    Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...

  9. Java for LeetCode 219 Contains Duplicate II

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

随机推荐

  1. Scrapy爬虫框架解析

    Scrapy框架解析 Scrapy框架大致包括以下几个组件:Scrapy Engine.Spiders.Scheduler.Item Pipeline.Downloader: 组件 Scrapy En ...

  2. Oracle总结第二篇【视图、索引、事务、用户权限、批量操作】

    前言 在Oracle总结的第一篇中,我们已经总结了一些常用的SQL相关的知识点了-那么本篇主要总结关于Oralce视图.序列.事务的一些内容- 在数据库中,我们可以把各种的SQL语句分为四大类- (1 ...

  3. ESC/POS打印控制命令

    0x00. Command Notation [Name]                        The name of the command. [Format]               ...

  4. MUI开发记录

    最近很久没有更新博客了,因为一直在学习前端h5 手机app的开发.曾经一度觉得自己css和js学得不错,进入到前端领域后才发现水很深~ HUuilder使用安卓模拟器 安卓模拟器有很多,我这里以夜神模 ...

  5. program 1 : python codes for login program(登录程序python代码)

    #improt time module for count down puase time import time #set var for loop counting counter=1 #logi ...

  6. 非常有用的css使用总结

    积小流以成江海,很多东西你不总结就不是你的东西 常用css总结: /*设置字体*/ @font-face { font-family: 'myFont'; src: url('../font/myFo ...

  7. 基于MyBatis3.0.6的基本操作介绍

    每 一 个 MyBatis 的 应 用 程 序 都 以 一 个 SqlSessionFactory 对 象 的 实 例 为 核 心 .SqlSessionFactory本身是由SqlSessionFa ...

  8. 认识 Java Message Service

    1. Java Message Service : 是一个消息服务的标准或者说是规范,允许应用程序组件基于JavaEE平台创建.发送.接收和读取消息. 实现Java 程序与MQ Server 之间互相 ...

  9. R语言画曲线图

    本文以1950年到2010年期间我国的火灾统计数据为例,数据如下所示: (0)加载数据 data<-read.csv("E:\\MyDocument\\p\\Data\\1950~20 ...

  10. SQL server2005学习笔记(一)数据库的基本知识、基本操作(分离、脱机、收缩、备份、还原、附加)和基本语法

    在软件测试中,数据库是必备知识,假期闲里偷忙,整理了一点学习笔记,共同探讨. 阅读目录 基本知识 数据库发展史 数据库名词 SQL组成 基本操作 登录数据库操作 数据库远程连接操作 数据库分离操作 数 ...