leetcode:Binary Search Tree:Contains Duplicate III(220)

原创 2016年08月31日 14:54:34

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.


class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        multiset<long long> bst;
        for (int i = 0; i < nums.size(); ++i) {
            if (bst.size() == k + 1) bst.erase(bst.find(nums[i - k - 1]));
            auto lb = bst.lower_bound(nums[i]);
            if (lb != bst.end() && abs(*lb - nums[i]) <= t) return true;
            auto ub = bst.upper_bound(nums[i]);
            if (ub != bst.begin() && abs(*(--ub) - nums[i]) <= t) return true;
            bst.insert(nums[i]);
        }
        return false;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Contains Duplicate III - LeetCode 220

题目描述: Given an array of integers, find out whether there are two distinct indices i and j in the ar...
  • bu_min
  • bu_min
  • 2015年06月01日 22:44
  • 419

[leetcode ]220.Contains Duplicate III

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

【LEETCODE】220-Contains Duplicate III

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

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

Leetcode220-Contains Duplicate III

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

220. Contains Duplicate III

Given an array of integers, find out whether there are two distinct indices i and j in the array s...
  • zshouyi
  • zshouyi
  • 2016年12月30日 06:40
  • 89

217, 219,220. Contains Duplicate I, II, III

Given an array of integers, find if the array contains any duplicates. Your function should return ...

[Leetcode 220, Medium] Contains Duplicates III

[Leetcode 220, Medium] Contains Duplicates III

[LeetCode]217. Contains Duplicate&219. Contains Duplicate II&220. Contains Duplicate III

217. Contains Duplicate 219. Contains Duplicate II 220. Contains Duplicate III

[LeetCode] Contains Duplicate III

Contains Duplicate III Given an array of integers, find out whether there are two distinct indices ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:Binary Search Tree:Contains Duplicate III(220)
举报原因:
原因补充:

(最多只允许输入30个字)