LeetCode 219. Contains Duplicate II (包含重复项之二)
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 (包含重复项之二)的更多相关文章
- [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 ...
 - [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
		
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
 - [LeetCode] Contains Duplicate II 包含重复值之二
		
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
 - LeetCode 217. Contains Duplicate (包含重复项)
		
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
 - [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 ...
 - C#解leetcode 219. Contains Duplicate II
		
该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...
 - 219 Contains Duplicate II 存在重复 II
		
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使 nums [i] = nums [j],并且 i 和 j 的绝对差值最大为 k. 详见:https://leetcod ...
 - LeetCode 219 Contains Duplicate II
		
Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...
 - 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 ...
 
随机推荐
- 聊聊React高阶组件(Higher-Order Components)
			
使用 react已经有不短的时间了,最近看到关于 react高阶组件的一篇文章,看了之后顿时眼前一亮,对于我这种还在新手村晃荡.一切朝着打怪升级看齐的小喽啰来说,像这种难度不是太高同时门槛也不是那么低 ...
 - sed命令基础
			
sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space ...
 - Jquery总结图
			
读完锋利Jquery第二版书,对其进行整理做出的思维导图:
 - JavaScript中的for in循环
			
在学习AJAX的时候,发现JavaScript中for in循环,这种循环对于遍历JSON是很好用的.于是写下了这篇博文 作用 for in循环本质上是forEach循环,它主要有两个作用 遍历数组 ...
 - CKEditor与dotnetcore实现图片上传
			
CKEditor的使用 1.引入js库 <script src="https://cdn.ckeditor.com/4.6.1/standard-all/ckeditor.js&quo ...
 - 【设计模式】module(模块)模式
			
写在前面 最近刚接触到设计模式, <head first设计模式>里有一篇文章,是说使用模式的心智, 1.初学者"心智" :"我要为HELLO WORLD找个 ...
 - CentOS 7安装squid代理服务器
			
Squid,一个高性能的代理缓存服务器,支持FTP.gopher.HTTP协议. Squid,一个缓存Internet 数据的软件,其接收用户的下载申请(作为代理服务器),并自动处理所下载的数据,并返 ...
 - sleep,yield,wait,notify,notifyAll
			
1.wait,notify,notifyAll是Object的方法.他们必须在同步块中使用,并且当前线程必须已经获取了锁.wait方法,用来释放同步块对象上的锁,并且等待其他的线程唤醒(notify) ...
 - ch340是什么芯片
			
CH340 是一个USB 总线的转接芯片,实现USB 转串口.USB 转IrDA 红外或者USB 转打印口. 在串口方式下,CH340 提供常用的MODEM联络信号,用于为计算机扩展异步串口, ...
 - Paint the Grid Again   ZOJ - 3780   拓扑
			
Paint the Grid Again Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu [ ...