https://leetcode.cn/problems/smallest-difference-lcci/description/

https://www.nowcoder.com/feed/main/detail/15457eb44fce4550a161bebef3e61e87

使用 Integer.MAX_VALUE 有溢出问题

image.png

class Solution {
 public int smallestDifference(int[] a, int[] b) {

        Arrays.sort(a);
        Arrays.sort(b);

        int i = 0,j = 0;
        long min = Long.MAX_VALUE;

        while (i < a.length && j <b.length) {
            if (a[i] == b[j]) return 0;
            else if (a[i] > b[j]) {
                min = Math.min(min,(long) a[i] -(long) b[j]);
                j ++;
            } else {
                min = Math.min(min,(long) b[j] -(long) a[i]);
                i ++;
            }
        }
        return (int)min;
    }
}

作者:大余
链接:<https://leetcode.cn/problems/smallest-difference-lcci/solutions/652664/javashuang-zhi-zhen-leetcode-day3-by-da-wr53o/>
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。