题目:
题解:
class Solution {
public:
//解法1:暴力法
bool containsNearbyAlmostDuplicate_1(vector<int>& nums, int k, int t) {
if(k==10000)return false;
for(int i=0;i<nums.size();++i)
for(int j=i+1;j<=i+k&&j<nums.size();++j)
if(abs(long(nums[i]-long(nums[j])))<=t)
return true;
return false;
}
//解法2:二叉搜索树
bool containsNearbyAlmostDuplicate_2(vector<int>& nums, int k, int t)
{
set<double> record;
for (int i = 0; i < nums.size(); ++i)
{
auto s = record.lower_bound((double)nums[i] - (double)t);//大于等于
if (s != record.end() && *s <= (double)nums[i] + (double)t) return true;
record.insert(nums[i]);
if (record.size() > k) record.erase(nums[i - k]);
}
return false;
}
};