一、最小差
给定两个整数数组a和b,计算具有最小差绝对值的一对数值(每个数组中取一个值),并返回该对数值的差。
实例:
输入:{1, 3, 15, 11, 2}, {23, 127, 235, 19, 8}
输出: 3,即数值对(11, 8)
提示:
1 <= a.length, b.length <= 100000
-2147483648 <= a[i], b[i] <= 2147483647
正确结果在区间[-2147483648, 2147483647]内
【注意】正负相减可能会超范围,两个for循环可能会超时!
class Solution {
public int smallestDifference(int[] a, int[] b) {
Arrays.sort(a);
Arrays.sort(b);
int len1 = a.length;
int len2 = b.length;
long res = Integer.MAX_VALUE;
int i = 0;
int j = 0;
while(i<len1 && j<len2){
if(a[i] <= b[j]){
res = Math.min(res,(long)b[j] - (long)a[i]);
i++;
}else{
res = Math.min(res,(long)a[i] - (long)b[j]);
j++;
}
}
return (int)res;
}
}