220. Contains Duplicate III
题目:
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k.
链接: http://leetcode.com/problems/contains-duplicate-iii/
题解:
一开始的想法是用类似Contains Duplicate II的方法,不过好像不好写。于是研究了一下Discuss,发现有大概三种解法。
- Sort the array, keep original index
- TreeMap
- Bucket sliding window.
最后暂时只做了第一种方法。排序以后再查找,这里需要自己定义一个数据结构,implements Comparable,并且还有一个inRangeTWith method来比较两个节点的原index是否在t范围内。 二刷一定要补上第二和第三种。
Time Complexity - O(n2) , Space Complexity - O(n)
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(nums == null || nums.length < 2 || t < 0 || k < 0)
return false;
ValueWithIndex[] arr = new ValueWithIndex[nums.length];
for(int i = 0; i < nums.length; i++)
arr[i] = new ValueWithIndex(nums[i], i);
Arrays.sort(arr);
for(int i = 0; i < arr.length; i++) {
for(int j = i + 1; (j < arr.length) && (arr[j].inRangeTWith(arr[i], t)); j++) {
if(Math.abs(arr[i].index - arr[j].index) <= k)
return true;
}
}
return false;
}
private class ValueWithIndex implements Comparable<ValueWithIndex> {
public int value;
public int index;
public ValueWithIndex(int val, int i) {
this.value = val;
this.index = i;
}
public int compareTo(ValueWithIndex v2) {
if(value < 0 && v2.value >= 0)
return -1;
if(value >= 0 && v2.value < 0)
return 1;
return value - v2.value;
}
public boolean inRangeTWith(ValueWithIndex v2, int t) { // value always >= v2.value
if(value == v2.value)
return true;
if(value >= 0 && v2.value >= 0)
return value - v2.value <= t;
else if(value < 0 && v2.value < 0)
return value <= v2.value + t;
else
return value - t <= v2.value;
}
}
}
二刷:
Java:
使用类似Contains Duplicates II的方法。 Time Complexity - O(n ^ 2) TLE, Space Complexity - O(n)
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (nums == null || nums.length < 2) return false;
Set<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
for (int num : set) {
if (Math.abs(num - nums[i]) <= t) return true;
}
set.add(nums[i]);
if (i >= k - 1) set.remove(i - k + 1);
}
return false;
}
}
新建数据结构先排序再查找。
- 这里我们先构建一个Node class包含val和index
- 根据数组,用nums[i]和i作为pair创建Node数组
- 对Node数组使用nums[i]进行排序
- 使用sliding window进行比较,这个部分比较的复杂度其实是O(n)
要注意我们的Node class继承了Comparable<Node> interface, 注意写compareTo() 和 inRangeTWith() 这两个方法时的一些边界条件地处理。
Time Complexity - O(nlogn) , Space Complexity - O(n)
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (nums == null || nums.length < 2 || t < 0 || k < 0) return false;
int len = nums.length;
Node[] nodes = new Node[len];
for (int i = 0; i < len; i++) nodes[i] = new Node(nums[i], i);
Arrays.sort(nodes);
for (int i = 0; i < len; i++) {
for (int j = i + 1; (j < len) && (nodes[j].inRangeTWith(nodes[i], t)); j++) {
if (Math.abs(nodes[i].index - nodes[j].index) <= k) return true;
}
}
return false;
}
private class Node implements Comparable<Node> {
public int val;
public int index;
public Node(int val, int i) {
this.val = val;
this.index = i;
}
public int compareTo(Node n2) {
if (val < 0 && n2.val >= 0) return -1;
if (val >= 0 && n2.val < 0) return 1;
return val - n2.val;
}
public boolean inRangeTWith(Node n2, int t) { // value always >= v2.value
if(val == n2.val) return true;
if((val >= 0 && n2.val >= 0) || (val < 0 && n2.val < 0)) {
return val - n2.val <= t;
} else {
return val <= n2.val - t;
}
}
}
}
Reference:
https://leetcode.com/discuss/65056/java-python-one-pass-solution-o-n-time-o-n-space-using-buckets
https://leetcode.com/discuss/47974/java-treeset-implementation-nlogk
https://leetcode.com/discuss/52545/java-solution-without-dictionary-sort-nums-record-positions
https://leetcode.com/discuss/39216/ac-short-java-solution-using-treeset-and-subset-function
https://leetcode.com/discuss/38206/ac-o-n-solution-in-java-using-buckets-with-explanation
https://leetcode.com/discuss/38177/java-o-n-lg-k-solution
https://leetcode.com/discuss/38148/my-o-n-accepted-java-solution-using-hashmap
https://leetcode.com/discuss/38146/java-solution-with-treeset
220. Contains Duplicate III的更多相关文章
- [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 ...
- Java for LeetCode 220 Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- (medium)LeetCode 220.Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 【LeetCode】220. Contains Duplicate III
题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- [Leetcode] 220. Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 【medium】220. Contains Duplicate III
因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器. Given an array of integers, find out whether there ar ...
- 220. Contains Duplicate III 数组指针差k数值差t
[抄题]: Given an array of integers, find out whether there are two distinct indices i and j in the arr ...
- 220 Contains Duplicate III 存在重复 III
给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 nums [i] 和 nums [j] 的绝对差值最大为 t,并且 i 和 j 之间的绝对差值最大为 k. 详见:https://le ...
- LeetCode 220. Contains Duplicate III (分桶法)
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
随机推荐
- 洛古 P1373 小a和uim之大逃离
P1373 小a和uim之大逃离 题目提供者lzn 标签 动态规划 洛谷原创 难度 提高+/省选- 题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北部天边急涌过来,还伴着一道道闪电 ...
- uva247 - Calling Circles(传递闭包+DFS)
题意:两人相互打电话(直接或间接),则在一个电话圈.即a给b打电话,b给c打电话,则a给c间接打电话. 注意:1.注意标记.2.注意输出格式. #include<iostream> #in ...
- 抓包分析TCP的三次握手和四次分手
一:三次握手 三次的握手的过程是: 1.由发起方HostA向被叫方HostB发出请求报文段,此时首部中的同步位SYN=1,同时选择一个序列号seq=x.TCP规定,SYN报文(即SYN=1的报文段)不 ...
- [原创] linux课堂-学习笔记-目录及概况
本学习笔记基于:网易云课堂-linux课堂 课时1Centos 6.4安装讲解46:14 课时2Centos 6.4桌面环境介绍与网络连接04:30 课时3 Linux目录结构介绍及内核与shell分 ...
- 符合web标准的网页下拉菜单
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> <head> < ...
- 64位CentOS 6.4下安装wine(32位)
1. 到http://dl.fedoraproject.org/pub/epel/6/x86_64/repoview/epel-release.html下载epel-release-6-8.noarc ...
- PHP去掉转义后字符串中的反斜杠\函数stripslashes
addslashes函数主要是在字符串中添加反斜杠对特殊字符进行转义,stripslashes则是去掉转义后字符串中的反斜杠\,比如当你提交一段 json数据到PHP端的时候可能会遇到json字符串中 ...
- Jqplot在joomla组件中的应用
(1)在com_collect组件中采用的是ajax获取json类型的值.[http://www.jqplot.com/tests/data-renderers.php]这上边有实例. (2)在jqp ...
- POJ 2411 压缩状态DP
这个题目非常赞! 给定一个矩形,要求用1*2 的格子进行覆盖,有多少种覆盖方法呢? dp[i][j] 当状态为j,且第i行已经完全铺满的情况下的种类数有多少种?j中1表示占了,0表示没有被占. 很显然 ...
- crystal report format number
ToText({#totalPrice}, 2,'.', ',') &" €" http://crystaltricks.com/wordpress/?p=149