首页 > 其他 > 详细

220. Contains Duplicate III

时间:2016-12-05 07:50:36      阅读:202      评论:0      收藏:0      [点我收藏+]

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.

维护一个windon

treeset:

ceiling(E e)

Returns the least element in this set greater than or equal to the given element, or null if there is no such element.
 
floor(E e)
Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.
 
remove(Object o)
Removes the specified element from this set if it is present.
 
public class Solution {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        if(nums.length < 2 || t < 0) return false;
        TreeSet<Integer> set = new TreeSet<Integer>();
        for(int i = 0 ; i < nums.length; i++){
            Integer floor = set.floor(nums[i] + t);
            Integer ceil = set.ceiling(nums[i] - t);
            if(floor != null && floor >= nums[i] || ceil != null && ceil <= nums[i])
                return true;
            set.add(nums[i]);
            if(i >= k) set.remove(nums[i-k]);
        }
        return false;
    }
}

 

220. Contains Duplicate III

原文:http://www.cnblogs.com/joannacode/p/6132588.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!