基本就是遍历的过程中不断的用hash表进行判断,然后不断的维持着这个仍然在范围内的集合。
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
map<long long, int> m;
int j = 0;
for (int i = 0; i < nums.size(); ++i) {
if (i - j > k && m[nums[j]] == j) m.erase(nums[j++]);
auto a = m.lower_bound(nums[i] - t);
if (a != m.end() && abs(a->first - nums[i]) <= t) return true;
m[nums[i]] = i;
}
return false;
}
};