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 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.
解题思路:
暴力枚举会LTE,解题思路是用sortedSet来解决,sortedSet可以返回一个在某个范围的subSet,同时判断这个subSet是否为空即可解决,主要,本题需要使用long类型!
JAVA实现如下:
import java.util.*;
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(nums==null || nums.length<2||k<1 || t<0)
return false;
SortedSet<Long> set = new TreeSet<Long>();
for(int j=0; j<nums.length; j++) {
SortedSet<Long> subSet = set.subSet((long)nums[j]-t, (long)nums[j]+t+1);
if(!subSet.isEmpty())
return true;
if(j>=k)
set.remove((long)nums[j-k]);
set.add((long)nums[j]);
}
return false;
}
}
Java for 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 ...
- (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 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 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java实现 LeetCode 220 存在重复元素 III(三)
220. 存在重复元素 III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最 ...
- 【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 ...
随机推荐
- 【转】Flume日志收集
from:http://www.cnblogs.com/oubo/archive/2012/05/25/2517751.html Flume日志收集 一.Flume介绍 Flume是一个分布式.可 ...
- Emmet语法实例(帮助快速开发)
写完命令后按键 tab 就可以生成了. 应用于大多数已经内置或可以安装emmet的编辑器下级元素命令 > <!--div>p--> <div> <p>& ...
- hdu.1198.Farm Irrigation(dfs +放大建图)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- informatica通用命令方式启动workflow
#传mapping参数时#调用执行workflow,-sv informatica服务,-d 域 -u 用户,-p 密码 -folder知识库下的workflow所在文件夹,-wait wf_test ...
- Mac 上真正替换LiveWriter 的工具 - ecto
Mac 上真正替换LiveWriter 的工具 - ecto 13年开始使用mac.而后想把 windows 替换到.一直在寻找LiveWriter 的工具,至今终于找到 我先感谢这位博主 http: ...
- boss设计参考的脑图
- Linux(Ubuntu)安装并破解 SecureFX
根据Linux(Ubuntu)下面SecureCRT 完全破解(地址:http://www.boll.me/archives/680) 添加了Linux(Ubuntu)下面SecureFX 完全破解, ...
- JS中的prototype(原文地址:http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html)
JS中的phototype是JS中比较难理解的一个部分 本文基于下面几个知识点: 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个 ...
- java之远程接口调用
一.通过地址栏传值 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...
- 分布式系统一致性算法 raft学习
在学习MongoDB的过程中,有博客中写道其搭建复制集时使用了raft算法,经过简单地的搜索资料后,发现了一个特别好的网站资料.这个网站用动画的形式,非常清楚和详尽的解释了整个raft算法的精要和过程 ...