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.

题目大意:给定一个数组,找出是否存在两个不同下标的值相差<=t,下标i和j相差<=k。

解题思路:题目没说数组有序,那就得按照无序处理,可以排序,然后从头遍历;或者用个BST之类的有序数据结构,遍历数组的时候重建一下,重建的过程中判断是否有合法的解,有就返回true,否则重建完之后返回false。

public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(nums==null||nums.length==0||k<=0){
return false;
}
TreeSet<Integer> tree = new TreeSet<>();
for(int i=0;i<nums.length;i++){
Integer big = tree.floor(nums[i]+t);//floor返回集合里<=指定元素的最大值
Integer small = tree.ceiling(nums[i]-t);//celing返回集合里>=指定元素的最小值
if((big!=null&&big>=nums[i])||(small!=null&&small<=nums[i])){
return true;
}
if(i>=k){
tree.remove(nums[i-k]);
}
tree.add(nums[i]);
}
return false;
}
}

Contains Duplicate III —— LeetCode的更多相关文章

  1. Contains Duplicate III -leetcode

    Contains Duplicate III Given an array of integers, find out whether there are two distinct indices i ...

  2. Contains Duplicate III

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

  3. Contains Duplicate,Contains Duplicate II,Contains Duplicate III

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  4. [LeetCode] Contains Duplicate III 包含重复值之三

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

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

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

  7. [Leetcode] Contains Duplicate III

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

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

  9. LeetCode——Contains Duplicate III

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

随机推荐

  1. 关于pv的那些事!!

    遗留问题:whid=1969的日志记录是什么意思? 网站站点信息未分配的时候,会用1969去代替站点信息. PV:页面浏览量(page view),用户每次打开或刷新一次网页即被计算一次. 关于pv的 ...

  2. .Net实现IO操作

    IO操作需要的web.config里的节点配置 <configuration>  <appSettings>    <!--上传文件类型要求-->    <a ...

  3. android 数字键盘制作

    //布局相关<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android ...

  4. jquery ui 插件------------------------->sortable

    <!doctype html><html lang="en"><head>  <meta charset="utf-8" ...

  5. iPhone中如何判断当前相机是否可用

    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; if (![UIImag ...

  6. Sqoop import加载HBase案例详解

    简单写一下如何将订单表sqoop到hbase表中的步骤. 下表: 1.通过hbase shell 打开hbase. 2.创建一个hbase表 create 'so','o' 3.将so表的数据导入到h ...

  7. 【转】 wpf系列-入门

    转自:http://www.cnblogs.com/huangxincheng/category/388852.html   8天入门wpf—— 第八天 最后的补充 摘要: 从这一篇往前看,其实wpf ...

  8. 完整的 dataType=text/plain jquery ajax 登录验证

    Html: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <m ...

  9. TabControl控件中TabPage的显示和隐藏

    TabPage里面含有方法Hide和Show,但没有任何作用,实际隐藏和显示需要使用如下2个方法 方法一:此方法比较简单 TabPageServo.Parent = Nothing   //隐藏 Ta ...

  10. jQuery幻灯片skitter-slider插件学习总结

    @(关键词)[skitter|jquery|网页幻灯片|slider] Skitter 是一个非常酷炫的jQuery网页幻灯片插件,支持非常多种酷炫幻灯片切换方式,下载前往官网,另有参考文档 下面简单 ...