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.

 

ref: http://bookshadow.com/weblog/2015/06/03/leetcode-contains-duplicate-iii/

好牛逼 “滑动窗口”  这里的做法

,新学了一个结构 treeset

下面引用书影的解释“

解法II:“滑动窗口” + TreeSet

参考LeetCode Discuss:https://leetcode.com/discuss/38177/java-o-n-lg-k-solution

TreeSet数据结构(Java)使用红黑树实现,是平衡二叉树的一种。

该数据结构支持如下操作:

1. floor()方法返set中≤给定元素的最大元素;如果不存在这样的元素,则返回 null。

2. ceiling()方法返回set中≥给定元素的最小元素;如果不存在这样的元素,则返回 null。

有个容易八阿哥的地方

n<=t+set.floor(n))

不能写成n-t《=。。。

public class Solution {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        
        if(k<1||t<0) return false;
        TreeSet<Integer> set = new TreeSet<Integer>();
        for(int i=0;i<nums.length;i++){
            int n = nums[i];
            if((set.floor(n)!=null&& n<=t+set.floor(n))||(set.ceiling(n)!=null && set.ceiling(n)<=t+n))
                return true;
            set.add(n);
            if(i>=k)
                set.remove(nums[i-k]);
        }
        return false;
            
    }
}

 

posted @ 2015-06-13 00:48  世界到处都是小星星  阅读(1616)  评论(0编辑  收藏  举报