1200. 最小绝对差

题目

给你个整数数组 arr,其中每个元素都 不相同

请你找到所有具有最小绝对差的元素对,并且按升序的顺序返回。

示例1:

1
2
输入:arr = [4,2,1,3]
输出:[[1,2],[2,3],[3,4]]

示例2:

1
2
输入:arr = [1,3,6,10,15]
输出:[[1,3]]

示例3:

1
2
输入:arr = [3,8,-10,23,19,-4,-14,27]
输出:[[-14,-10],[19,23],[23,27]]

提示:

  • 2 <= arr.length <= 10^5
  • -10^6 <= arr[i] <= 10^6

解答

解法一:

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public List<List<Integer>> minimumAbsDifference(int[] arr) {
Arrays.sort(arr);

List<List<Integer>> result = new ArrayList<>();
int min = Integer.MAX_VALUE;
for (int i = 0;i < arr.length - 1;i++) {
int diff = arr[i + 1] - arr[i];
if (diff < min) {
result.clear();
result.add(Arrays.asList(arr[i], arr[i + 1]));
min = diff;
} else if (diff == min) {
result.add(Arrays.asList(arr[i], arr[i + 1]));
}
}
return result;
}
0%