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 difference between i and jis at most k.
链接: http://leetcode.com/problems/contains-duplicate-ii/
题解:
用HashMap来保存(nums[i],i) pair,假如存在相同key并且 i - map.get(key) <= k,返回true。否则遍历完毕以后返回false.
Time Complexity - O(n), Space Complexity - O(n)
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
if(nums == null || nums.length == 0)
return false;
Map<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < nums.length; i++) {
if(map.containsKey(nums[i])) {
if(i - map.get(nums[i]) <= k)
return true;
}
map.put(nums[i], i);
} return false;
}
}
二刷:
跟一刷一样,使用一个Map来保存数字以及数字的index,然后进行比较。假如相同并且 i - map.get(nums[i]) <= k,那么我们返回true。否则遍历完毕以后返回false.
Java:
Time Complexity - O(n), Space Complexity - O(n)
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
if (nums == null || nums.length == 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;
}
map.put(nums[i], i);
}
return false;
}
}
三刷:
方法还是和一刷,二刷一样,用一个HashMap来保存元素和位置。这里最好给HashMap设定一个初始的大小来避免resizing带来的cost。最大的test case好像是30000,我们设置30000 / 0.75 = 40000左右就可以了。
Java:
Time Complexity - O(n), Space Complexity - O(n)
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
if (nums == null || k <= 0) {
return false;
}
Map<Integer, Integer> map = new HashMap<>(41000);
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) {
return true;
}
map.put(nums[i], i);
}
return false;
}
}
使用Set以及sliding window。
- 我们维护一个size为k的HashSet
- 遍历整个数组,每次先判断是否有重复,有重复的话我们直接返回true
- 当size > k的时候,这时候size() = k + 1。 我们要把距离当前元素最远的一个元素,即第i - k个元素移出set,继续维护set的size = k
- 全部遍历完毕以后返回false。
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
if (nums == null || k <= 0) {
return false;
}
Set<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
if (!set.add(nums[i])) {
return true;
}
if (set.size() > k) {
set.remove(nums[i - k]);
}
}
return false;
}
}
Update:
利用HashMap的put。创建map的时候利用load factor = 0.75,我们建立一个比nums.length略大一些的map就能节约resizing的时间,但这也是空间换时间了。
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
if (nums == null || k <= 0) return false;
Map<Integer, Integer> map = new HashMap<>((int)(nums.length / 0.8));
for (int i = 0; i < nums.length; i++) {
if (!map.containsKey(nums[i])) map.put(nums[i], i);
else if (i - map.put(nums[i], i) <= k) return true;
}
return false;
}
}
Update:重写了使用set的Sliding Window方法
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
if (nums == null || k <= 0) return false;
Set<Integer> set = new HashSet<>((int)(nums.length / 0.8));
for (int i = 0; i < nums.length; i++) {
if (!set.add(nums[i])) return true;
if (set.size() > k) set.remove(nums[i - k]);
}
return false;
}
}
Update: SouthPenguin的Sliding window方法。 最优解。
Worst case: Time Complexity - O(n), Space Complexity - O(n)
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
if (nums == null || k <= 0) return false;
Set<Integer> set = new HashSet<>((int)(nums.length / 0.8));
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;
}
}
Reference:
https://leetcode.com/discuss/38445/simple-java-solution
219. Contains Duplicate II的更多相关文章
- 219. Contains Duplicate II【easy】
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...
- 219. Contains Duplicate II - LeetCode
Question 219. Contains Duplicate II Solution 题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false 思路:用一个map记录每 ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- [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 ...
- [刷题] 219 Contains Duplicate II
要求 给出整型数组nums和整数k,是否存在索引i和j,nums[i]==nums[j],且i和j之间的差不超过k 思路 暴力解法(n2) 建立最长为k+1的滑动窗口,用set查找窗口中是否有重复元素 ...
- 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 ...
- Leetcode 219 Contains Duplicate II STL
找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...
- 【leetcode❤python】 219. Contains Duplicate II
#-*- coding: UTF-8 -*-#遍历所有元素,将元素值当做键.元素下标当做值#存放在一个字典中.遍历的时候,#如果发现重复元素,则比较其下标的差值是否小于k,#如果小于则可直接返回Tru ...
- (easy)LeetCode 219.Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i ...
随机推荐
- java如何调用webservice接口
java调用WebService可以直接使用Apache提供的axis.jar自己编写代码,或者利用Eclipse自动生成WebService Client代码,利用其中的Proxy类进行调用.理论上 ...
- DP入门数塔问题
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 已经告诉你了,这 ...
- java学习笔记_MIDI_GUI
import javax.sound.midi.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; class M ...
- MySQL学习笔记之数据存储类型
说明:本文是作者对MySQL数据库数据存储类型的小小总结. Numeric Type (数字类型) 1.TINYINT.SMALLINT.MEDIUMINT.INT.BIGINT主要根据存储字节长度不 ...
- Mac OS X安装OpenCV 3.1.0
在我的上一篇文章“”中已经介绍了Linux下OpenCV的安装配置方法,在这里仅仅记录Mac上相对于Linux的一点点差异. 1. 安装依赖包 Mac上安装软件包使用的工具是brew,用此来替代Ubu ...
- Transact-SQL 存储过程(c#调用执行)
1. Microsoft SQL Server Management Studio 中创建 存储过程 1.1 借助模板资源管理器中的Stored Procedure模板进行修改创建 1.2 直接新建查 ...
- grails的插件
今天来歪理邪说一下grails的插件. 有个问题让本人困惑了一段时间,插件是属于grails的,还是属于某个工程的?为什么会有这个问题呢,这涉及到grails插件的安装方式. grails的插件像是一 ...
- IOS 四种保存数据的方式
在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍一下数据保存的方式: 1.NSKeyed ...
- 添加打印机的时候怎样说windows没法连接到打印机毛病为0x00000002
把PrinterExtensionsandNotifications这个服务启动1下试试 PrintSpooler服务停止然后再启用试试
- Android模拟神器Genymotion eclipse插件安装问题出解决
我之前一直是打开eclipse之前直接运行Genymotion模拟器就可以连接到adb了,非常方便,但最近突然想来装个eclipse的Genymotion插件玩玩,安装时居然出错了,于是不折腾好心里不 ...