1385.两个数组间的距离值
我的思路是:
首先定义一个布尔函数,判断arr1中的一个元素对于arr2是否符合距离规范。
再在findTheDistanceValue 函数中遍历arr1中的每一个元素,若符合规范,count++
最后的距离值就是count
bool isMeetDistance(int target, int* arr, int arrSize,int distance)
{
for (int i = 0; i < arrSize; i++)
{
int judge = target - arr[i];
if (judge < 0)
judge = -judge;
if (judge <= distance)
return false;
}
return true;
}
int findTheDistanceValue(int* arr1, int arr1Size, int* arr2, int arr2Size, int d) {
int count = 0;
for (int i = 0; i < arr1Size; i++)
{
if (isMeetDistance(arr1[i], arr2, arr2Size, d) == true)
count++;
}
return count;
}
该代码实现了一个功能,检查一个数组中的元素是否与另一个数组中的所有元素之间的差值大于给定的距离。如果满足条件,函数isMeetDistance返回true,然后在findTheDistanceValue函数中计数。最后返回不满足条件的元素数量。

242

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



