LeetCode 220. Contains Duplicate III

参考了Eason Liu的博文,代码:

class Solution 
{
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) 
    {
        multiset<long long> bst;
        for (size_t i = 0; i < nums.size(); ++ i)
        {
        	if (bst.size() == k+1)
        	{
        		bst.erase(nums[i-k-1]);
        	}
        	auto lb = bst.lower_bound(nums[i]-t);        	
        	if (lb!=bst.end() && *lb-nums[i]<=t)
        	{
        		return true;
        	}
        	bst.insert(nums[i]);
        }
        return false;
    }
};


你可能感兴趣的:(LeetCode,C++)