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. Java:final、static关键字 详解+两者结合使用

    一  final关键字 1) 关于final的重要知识点 final关键字可以用于成员变量.本地变量.方法以及类. final成员变量必须在声明的时候初始化或者在构造器中初始化,否则就会报编译错误. ...

  2. find命令基础讲解

    目录 前言 find命令 查找条件 指定搜索层级 根据文件名和inode查找 根据属名属主查找 根据文件类型查找 组合条件 根据文件大小查找 根据时间戳查找 根据权限查找 动作处理 参数替换xargs ...

  3. Spring-Boot:6分钟掌握SpringBoot开发

    构建项目 从技术角度来看,我们要用Spring MVC来处理Web请求,用Thymeleaf来定义Web视图,用Spring Data JPA来把阅读列表持久化到数据库里,姑且先用嵌入式的H2数据库. ...

  4. 枚举类TimeUnit

    枚举类TimeUnit 全路径为 java.util.concurrent.TimeUnit TimeUnit 主要用于通知基于时间的方法如何解释给定的计时参数 举例如下 如果 lock 不可用,则以 ...

  5. 跨Storyboard调用

    在开发中我们会有这种需求从一个故事板跳到另一个故事板 modal UIStoryboard *secondStoryboard = [UIStoryboard storyboardWithName:@ ...

  6. A glimpse of Support Vector Machine

    支持向量机(support vector machine, 以下简称svm)是机器学习里的重要方法,特别适用于中小型样本.非线性.高维的分类和回归问题.本篇希望在正篇提供一个svm的简明阐述,附录则提 ...

  7. spring 面向切面(AOP)

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP与OOP是面向不同领域的两种设计思想. ...

  8. 关于select的一个错误---属性选择器

    错误: jquery 获取下拉框 text='1'的 option 的value 属性值  我写的var t= $("#selectID option[text='1']).val() ; ...

  9. 详解MySQL基准测试和sysbench工具

    前言 作为一名后台开发,对数据库进行基准测试,以掌握数据库的性能情况是非常必要的.本文介绍了MySQL基准测试的基本概念,以及使用sysbench对MySQL进行基准测试的详细方法. 文章有疏漏之处, ...

  10. 分享基于分布式Http长连接框架--代码模型

    好的代码应该是方便客户端使用,代码能够自描述,规范化,大众标准化. 而且我相信代码也是有生命的,需要不断的维护它,你以什么样的态度对待它,它就会以同样的态度回敬你,所以在写代码前,先摆好自己的态度(一 ...