http://www.nowcoder.com/practice/1b2c9a2ba11746958036b29f2e9ee72b

```class Solution {
public:
vector<int> solve(int n, int m, vector<int> a, vector<int> x)
{
vector<int> myt(m); // 声明数组
for(int i = 0; i < m; ++i)
{
myt[i] = 2e9;
for(int j = 0; j < n; ++j) myt[i] = min(myt[i], abs(x[i]-a[j])); // 更新结果
}
return myt; // 返回结果
}
};```

```class Solution {
public:
vector<int> solve(int n, int m, vector<int> a, vector<int> x)
{
sort(a.begin(), a.end()); // 快排
vector<int> t(m);
for(int i = 0; i < m; ++i)
{
int myval = x[i];
int myans = 2e9;
if(myval < a[0])
myans = a[0]-myval; // 记录返回值
else
{
int l = 0, r = n-1;
while(l <= r)
{   //二分法的思想
int mid = (r+l)>>1;
if(a[mid] <= myval)
{
myans = min(myans, myval-a[mid]);
l = mid+1;
}
else
r = mid-1;
}
}
if(myval > a[n-1])
myans = min(myans, myval-a[n-1]);
else
{
int l = 0, r = n-1;
while(l <= r)
{   //二分法的思想
int mid = (r+l)>>1;
if(a[mid] >= myval){myans = min(myans, a[mid]-myval); r = mid-1;}
else l = mid+1;
}
}
t[i] = myans;
}
return t; // 返回结果即可
}
};```

01-04 15:28

2022-12-16 20:33

2022-12-08 23:34

2022-12-30 15:06

2022-12-14 17:42

02-01 14:32

2022-12-29 10:23