LeetCode 面试题 16.06. 最小差
题目描述
思路分析
双指针
参考代码
1
write your code here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int smallestDifference(int[] a, int[] b) {
Arrays.sort(a);
Arrays.sort(b);
int i = 0, j = 0;
long res = Long.MAX_VALUE;
while (i < a.length && j < b.length) {
long diff = a[i] - b[j];
res = Math.min(res, Math.abs(diff));
if (diff < 0) {
i++;
} else {
j++;
}
}
return (int) res;
}
}
CC BY-NC-SA 4.0
许可协议,转载请注明出处!
本博客所有文章除特别声明外,均采用