//示例中有整形溢出的问题,代码中强制转换为long long
//使用了ceil操作(在容器中为lower_bound)
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if (nums.size() == 1 || nums.size() == 0) return false;
set<long long> record;
int N = nums.size(), left = 0, right = min(N-1, k);
record.insert(nums[left]);
for (int i = 1; i <= right; i++) {
if (record.lower_bound((long long)nums[i] - (long long)t) != record.end() && *record.lower_bound((long long)nums[i] - (long long)t) <= (long long)nums[i] + (long long)t) return true;
else record.insert(nums[i]);
}
while (right < N-1) {
record.erase(nums[left++]);
if (record.lower_bound((long long)nums[++right] - (long long)t) != record.end() && *record.lower_bound((long long)nums[right] - (long long)t) <= (long long)nums[right] + (long long)t) return true;
else record.insert(nums[right]);
}
return false;
}
};
int main()
{
Solution sol;
vector<int> input = {0,2147483647};
int k = 1, t = 2147483647;
cout << sol.containsNearbyAlmostDuplicate(input, k, t);
return 0;
}