# 题目

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.

# AC代码

class Solution {
public:
//方法一，TLE
bool containsNearbyAlmostDuplicate1(vector<int>& nums, int k, int t) {
if (nums.empty())
return false;

int sz = nums.size();
//使用容器unordered_set 其查找性能为常量
unordered_set<int> us;
int start = 0, end = 0;
for (int i = 0; i < sz; ++i)
{
int len = us.size();
for (int j = 0; j < len; ++j)
{
int tmp = abs(nums[j] - nums[i]);
if (tmp <= t)
return true;
}//for
us.insert(nums[i]);
++end;

if (end - start > k)
{
us.erase(nums[start]);
++start;
}
}//for
return false;

}
//方法二：
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if (nums.empty())
return false;

int sz = nums.size();
map<long long, int > m;
int start = 0;
for (int i = 0; i < sz; ++i)
{
if (i - start >k && m[nums[start]] == start)
m.erase(nums[start++]);

auto a = m.lower_bound(nums[i] - t);
if (a != m.end() && abs(a->first - nums[i]) <= t)
return true;
//将元素值和下标插入map
m[nums[i]] = i;
}//for
return false;
return false;

}
};

GitHub测试程序源码

举报原因： 您举报文章：LeetCode（220） Contains Duplicate III 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)