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 ...
随机推荐
- poj 3616 Milking Time
Milking ...
- Poj 2262 / OpenJudge 2262 Goldbach's Conjecture
1.Link: http://poj.org/problem?id=2262 http://bailian.openjudge.cn/practice/2262 2.Content: Goldbach ...
- Adobe Dreamweaver CS6 序列号 注册码(转自91zcm)
Adobe Dreamweaver CS6 序列号 注册码: 1192-1632-4987-2281-1039-4334 1192-1697-0848-9963-2161-72481192-1871 ...
- 教您如何使用MySQL group_concat函数
MySQL group_concat函数是典型的字符串连接函数,下面就为您介绍MySQL group_concat的语法,希望对您学习MySQL group_concat函数有所帮助. MySQL g ...
- nginx的安装与配置
1.nginx的安装与配置 编译安装nginx需要实现安装开发包组“Development tools”和“Server Platform Development”.“Desktop Platform ...
- LCD/LED/OLED/等离子显示器区别
LCD液晶显示器: LCD(Liquid Crystal Display),其构造是在两片平行的玻璃当中放置液态的晶体(液晶),在玻璃后面,以CCFL冷光灯管(类似日光灯)作背光源.液晶的成像原理可以 ...
- get与post区别
在Ajax原理中,HTTP请求方法:get——用于获取数据(e.g. 浏览帖子):post——用于上传数据(e.g. 用户注册,上传图片) get 是在url里传数据,但这样做的安全性很差,并且容量不 ...
- 利用javascript调用摄像头,可以配合socket开发监控系统
<html> <head> <meta http-equiv="content-type" content="text/html; char ...
- push notification for iphone
由于公司业务需求,以前一直做PHP开发,突然让我研究push notification ,一下子迷糊啦,不知所措,抓狂!但是在自己的努力下还是初有成效!现拿出来显摆一下! 1:push notific ...
- JavaScript之this,new,delete,call,apply
OS:Window 8.1 关键字:JavaScript,HTML,this,new,delete,call,apply. 1.this 一般而言,在Javascript中,this指向函数执行时的当 ...