题目:

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,发现有大概三种解法。

  1. Sort the array, keep original index
  2. TreeMap
  3. 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;
}
}

新建数据结构先排序再查找。

  1. 这里我们先构建一个Node class包含val和index
  2. 根据数组,用nums[i]和i作为pair创建Node数组
  3. 对Node数组使用nums[i]进行排序
  4. 使用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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. (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 ...

  4. 【LeetCode】220. Contains Duplicate III

    题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  5. [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 ...

  6. 【medium】220. Contains Duplicate III

    因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器. Given an array of integers, find out whether there ar ...

  7. 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 ...

  8. 220 Contains Duplicate III 存在重复 III

    给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 nums [i] 和 nums [j] 的绝对差值最大为 t,并且 i 和 j 之间的绝对差值最大为 k. 详见:https://le ...

  9. 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 ...

随机推荐

  1. Excel常用函数

    1.基本的算数函数 sum() average() 2.三角函数 sin() cos() 3.

  2. uva439 - Knight Moves(BFS求最短路)

    题意:8*8国际象棋棋盘,求马从起点到终点的最少步数. 编写时犯的错误:1.结构体内没构造.2.bfs函数里返回条件误写成起点.3.主函数里取行标时未注意书中的图. #include<iostr ...

  3. OD常用断点

    OD常用断点 很全很全 常用断点 拦截窗口: bp CreateWindow 创建窗口 bp CreateWindowEx(A) 创建窗口 bp ShowWindow 显示窗口 bp UpdateWi ...

  4. 《SELinux安全上下文的管理(含图)》RedHat6.3——步骤详细、条理清晰

    1.为什么浏览器只识别/var/www/html下的文件? 2.为什么不识别别的目录下的index.html文件呢? 3.这里牵扯到身份证,先安装软件包. 4.打开selinux 5.建立一个新的目录 ...

  5. sersync 实时同步工具

    出处:http://code.google.com/p/sersync/ 当前版本的sersync依赖于rsync进行同步.如下图所示,在同步主服务器上开启sersync,将监控路径中的文件同步到目标 ...

  6. 解决ListView滑动时卡的问题,实现异步加载图片解决

    ListView是最为常见的空间之一,现在的应用的呈现形式大多数都需要用到ListView来呈现,以列表的方式最直观最便于操作. 那么在使用的过程中大家一定使用adapter适配器来匹配这个ListV ...

  7. [Linux]学习笔记(4)-su及passwd的用法介绍

    (1)su su命令用于将当前的用户切换到一个指定的用户.语法为: su - user_name 如果用户利用telnet方式远程登录,是不能直接以root账户登录的,也就是说在使用telnet登录服 ...

  8. DISCUZ! X2.5设置仅允许QQ登录注册论坛 加固会员注册机制

    论坛稍微有点起色之后,很多站长就会担心论坛经常被人恶意灌水.注册机.顶贴机等等一些列非法的手段.通常站长都会通过一些后台的设置和插件等等一 切有效的方法预防,但更多的站长会通过限制用户注册会员,需注册 ...

  9. 在win7上建立本地FTP站点详细步骤

    一.安装FTP组件点击:控制面板—>程序和功能—>打开或关闭Windows功能. 勾选“FTP服务器”及“FTP服务”“FTP扩展性”,点击“确定”,安装FTP组件. 勾选Web管理工具的 ...

  10. EXPLAIN句法 优化表结构

    EXPLAIN tbl_name or EXPLAIN SELECT select_options EXPLAIN tbl_name是DESC[RIBE] tbl_name或SHOW COLUMNS ...