资讯专栏INFORMATION COLUMN

220. Contains Duplicate III

王伟廷 / 1209人阅读

摘要:如果不是,则在相邻的两个内再找。如果相邻的内元素绝对值只差在以内,说明我们知道到了,返回为了保证,我们在时,删除对应的的元素都会落在里。为了解决这个问题,所有元素横移。

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
题意找到一组数,nums[i] and nums[j] 他们idx差值在k以内, 即 j - i <= k.
他们的差的绝对值在t以内,即 Math.abs(nums[i] - nums[j]) <= t.
我们可以构建一个大小为t+1的bucket, 比如[0, 1, 2, 3, ... , t] 最大绝对值差的两个数就是t和0. 
如果两个数字出现在同一个Bucket内,说明我们已经找到了。 如果不是,则在相邻的两个bucket内再找。
如果相邻的bucket内元素绝对值只差在t以内,说明我们知道到了,返回true.
为了保证j - i <= k,我们在i>=k时,删除 nums[i-k]对应的Bucket.
public class Solution {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        if( k < 1 || t < 0) return false;
        Map map = new HashMap<>();
        for(int i = 0; i < nums.length; i++){
            // [-t, 0] [0, t] 的元素都会落在bucket[0]里。
            // 为了解决这个问题,所有元素横移Integer.MIN_VALUE。
            long remappedNum = (long) nums[i] - Integer.MIN_VALUE;
            long bucket = remappedNum / ((long)t + 1);
            if(map.containsKey(bucket) 
                ||(map.containsKey(bucket-1) && remappedNum - map.get(bucket-1) <= t)
                    || (map.containsKey(bucket+1) && map.get(bucket+1) - remappedNum <= t) )
                    return true;
            if(i >= k) {
                long lastBucket = ((long) nums[i-k] - Integer.MIN_VALUE) / ((long)t + 1);
                map.remove(lastBucket);
            }
            map.put(bucket,remappedNum);
        }
        
        return false;
    }
}
TreeSet也可以解决这个问题, 我们首先需要把i-j <=k 的所有元素装起来, 
然后集合内的元素可以保证有序,在里面找最接近nums[i] +/- t的元素就行了。
[11,12,14,15]
tree.floor(13) = 12
tree.floor(12) = 12

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70090.html

相关文章

  • [LintCode/LeetCode] Contains Duplicate III

    Problem Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute dif...

    MageekChiu 评论0 收藏0
  • leetcode217.219.220 contains duplicate

    摘要:输入一个整数数组,查看数组中是否存在重复的值。新的数组中数组的下标为原数组的值,如果遍历过,则设置为。这里使用了作为实现的数据结构,通过堆的形式对集合中的数据进行存储,从而我们可以通过某种顺序获得该集合中的所有顺序。 217 Contains Duplicate Given an array of integers, find if the array contains any dup...

    tulayang 评论0 收藏0
  • [Leetcode] Contains Duplicate 包含重复

    摘要:代码集合法复杂度时间空间思路同样使用集合,但这次我们要维护集合的大小不超过,相当于是记录一个宽度为的窗口中出现过的数字。 Contains Duplicate I Given an array of integers, find if the array contains any duplicates. Your function should return true if any v...

    rozbo 评论0 收藏0
  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月汇总(100 题攻略)

    摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...

    tain335 评论0 收藏0

发表评论

0条评论

王伟廷

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<