文章目录
原题题目
代码实现(首刷自解)
class Solution {
public:
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
int ret = 0;
sort(arr2.begin(),arr2.end());
for(const auto& num:arr1)
{
auto higher_one = upper_bound(arr2.begin(),arr2.end(),num);
auto lower_one = lower_bound(arr2.begin(),arr2.end(),num);
if((higher_one != arr2.end() && *higher_one - num <= d) || (lower_one != arr2.end() && abs(num - *lower_one) <= d))
continue;
else
{
if(lower_one == arr2.begin() || abs(num - *(--lower_one) > d)) ++ret;
}
}
return ret;
}
};