LeetCode OJ 219.Contains Duplicate 2
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 jis at most k.
这个题目在前一个题的基础上有部分改动,对nums[i] = nums[j]出现的范围作出了规定,不是全局的,而是要求|i-j|<=k;
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer,Integer> map = new HashMap<>(0);
for(int i=0; i<nums.length; i++){
if(map.containsKey(nums[i])){
if(i-map.get(nums[i])<=k) return true;
}
if(map.size()>k) map.remove(nums[i-k]);
map.put(nums[i],i);
}
return false;
}
}
【二刷】二刷和一刷的变动不是很大,但是对HashMap的了解更深了。
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
if(nums.length <= 1 || k == 0) return false;
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++) {
if(map.containsKey(nums[i]) && i-map.get(nums[i]) <= k)
return true;
else map.put(nums[i], i);
}
return false;
}
}
【三刷】三刷的直接使用HashSet。遍历数组,如果当前下标已经大于k,则把set中不可能成立的数据删除掉,然后在把当前数字添加进去。如果set中已经包含该数,则会添加失败。
HashSet的add函数在添加成功返回true,在添加失败时返回false。
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Set<Integer> set = new HashSet<Integer>();
for(int i = 0; i < nums.length; i++){
if(i > k) set.remove(nums[i-k-1]);
if(!set.add(nums[i])) return true;
}
return false;
}
}
LeetCode OJ 219.Contains Duplicate 2的更多相关文章
- 【一天一道LeetCode】#219. Contains Duplicate II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】219. Contains Duplicate II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用set 使用字典 日期 题目地址:https:/ ...
- LeetCode OJ 220.Contains Duplicate 3
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- LeetCode OJ 217.Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- 【LeetCode】219. Contains Duplicate II
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- LeetCode OJ:Contains Duplicate III(是否包含重复)
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- LeetCode OJ:Contains Duplicate(是否包含重复)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
随机推荐
- 《精通CSS》——个人总结
[属性选择器] 属性选择器可以根据某个属性是否存在或属性的值来寻找元素. 只有在规定了 !DOCTYPE 时,IE7 和 IE8 才支持属性选择器.在 IE6 及更低的版本中,不支持属性选择. 事例: ...
- Python3与Python2的区别汇总
1.print 在Python3.0 是一个函数,正确输入应该是:print (3x) 2.raw_input 在Python3.0改成input
- 动态的改变标签内的src属性
<body> <ul> <li class='on'>1</li> <li>2</li> <li>3</li& ...
- MQ队列堆积太长,消费不过来怎么办(转)
转自:http://windwrite.com/archives/603 我们现有的业务就面临此问题,消息生产太快,消费不过来,导致队列堆积很长,把服务器内存耗尽,这时RabbitMQ的处理能力很低下 ...
- android 图片加载优化,避免oom问题产生
1,及时回收bitmap,在activity的onstop()和onDestory()里面调用如下代码进行bitmap的回收: // 先判断是否已经回收 if(bitmap != null & ...
- Android Studio Gradle project refresh failed No such property classpath for class
新建的一个 android 项目居然发现不能运行,gradle 无法启动,奇怪: Gradle 'Meitian' project refresh failed: No such p ...
- 理解Nodejs中的事件轮询机制
我在看<了不起的Nodejs>一书,阻塞与非阻塞IO那一章我来回看了N遍,然后...还是没太看懂..于是我找到了这篇日志,写的是真的有点好啊..潸然泪下.. 原文:http://www.r ...
- Java 集合 JDK1.7的LinkedList
Java 集合 JDK1.7的LinkedList @author ixenos LinkedList LinkedList是List接口的双向链表实现,JDK1.7以前是双向循环链表,以后是双向非循 ...
- 正方形网格 TRIANGLE_STRIP连接
unsigned int vIdx = 0, iIdx = 0; for (unsigned int stripRow = 0; stripRow < stripRows; stripRow++ ...
- OMCS开发手册(02) -- 多媒体连接器
OMCS开发手册(01) -- 多媒体设备管理器 一文,我们从Owner的角度详细描述了多媒体设备管理器的使用,本文我们将站在Guest的角度,描述OMCS中另一类组件/控件:多媒体连接器.多媒体连接 ...