(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 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.
思想借鉴:维持一个长度为k的window, 每次检查新的值是否与原来窗口中的所有值的差值有小于等于t的. 如果用两个for循环会超时O(nk). 使用treeset( backed by binary search tree) 的subSet函数,可以快速搜索. 复杂度为 O(n logk)
代码如下:
import java.util.SortedSet;
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(k<1 || t<0 ||nums==null ||nums.length<2) return false;
SortedSet<Long>set=new TreeSet<>();
int len=nums.length;
for(int i=0;i<len;i++){
SortedSet<Long>subSet=set.subSet((long)nums[i]-t,(long)nums[i]+t+1);
if(!subSet.isEmpty()) return true;
if(i>=k)
set.remove((long)nums[i-k]);
set.add((long)nums[i]);
}
return false;
}
}
运行结果:
(medium)LeetCode 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 ...
- [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 ...
- 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 ...
- 220. Contains Duplicate III
题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- 220 Contains Duplicate III 存在重复 III
给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使 nums [i] 和 nums [j] 的绝对差值最大为 t,并且 i 和 j 之间的绝对差值最大为 k. 详见:https://le ...
- 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 ...
随机推荐
- 制作OS X 10.10.3启动安装U盘
http://www.cnblogs.com/Bob-wei/p/4471407.html 1.获得“Install OS X Yosemite.app” 2.准备一个8GB的U盘,用磁盘工具“抹掉” ...
- 阿里云centos增加swap(虚拟内存)
我的阿里云服务器默认是没有配swap交换文件. 创建swap根目录 mkdir /swap 创建swap dd if=/dev/zero of=/swap/swap bs=1024 count=102 ...
- .NET去掉HTML标记
using System.Text.RegularExpressions; /// <summary> /// 去除HTML标记 /// </summary> /// < ...
- UI—代理简单使用
代理:又叫委托 通俗的说是自己不能办的事 委托给别人去办.比如UITextField,UIAlertView都使用了代理 写代理的步骤: 1.声明代理里面的协议方法(@protocl) 2.声明协议的 ...
- 10socket编程
这一节主要关注的还是粘包问题,我们利用recv实现一个recv_peek函数,它的目的是偷窥目的,它是利用recv的一个msg_peek参数与read的区别,read读取后 会擦除缓冲区的内容,而re ...
- 【Struts2学习笔记-6--】Struts2之拦截器
简单拦截器的使用 拦截器最基本的使用: 拦截方法的拦截器 拦截器的执行顺序 拦截结果的监听器-相当于 后拦截器 执行顺序: 覆盖拦截器栈里特定拦截器的参数 使用拦截器完成-权限控制 主要完成两个功能: ...
- [Debug]驱动程序调测方法与技巧
转自:http://blog.csdn.net/lichangc/article/details/43272457 驱动程序开发的一个重大难点就是不易调试.本文目的就是介绍驱动开发中常用的几种直接和间 ...
- 黄聪:怎么清理win7、win8更新垃圾(winsxs目录清理)
windows 系统(特别是Win8系统)在使用了一段时间后,发现C盘的空间降的好厉害,显然,有大量不该存在的文件还继续停留在硬盘里.究其原因,在于系统目录下的WinSxS目录占用了大量的空间!在我们 ...
- Linux 下 MySQL 的彻底卸载和安装配置字符集
前言: Linux环境下MySQL的安装和配置在网上已经有很多教程了.之所以写这篇文章是因为在配置字符集的时候找了网上的一些教程发现并不能用导致折腾了一阵子.下面的教程均是亲自实践. MySQL的彻底 ...
- U3d中实现A*寻路,附源文件
图片看不清楚,请点击看大图 http://pan.baidu.com/s/1pKwmOYn 写了好多,没保存,哎哎哎 空格键开始移动