From : https://leetcode.com/problems/contains-duplicate-iii/
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] andnums[j] is at most t and the difference between i and j is at most k.
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if(k<1 || t<0 || nums.size() < 2) return false;
multiset<int> box;
for(int i=0; i<nums.size()-1; ) {
box.insert(nums[i]);
i++;
set<int>::iterator it = box.lower_bound(nums[i]-t);
if(it!=box.end()) {
if(*it-nums[i]>=-t && *it-nums[i]<=t) return true;
}
if(i>=k) {
box.erase(nums[i-k]);
}
}
return false;
}
};
本文探讨了如何在数组中找到两个元素,它们之间的差值不超过指定阈值t,并且它们的索引之差不超过k。
112

被折叠的 条评论
为什么被折叠?



