-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimumAbsoluteDifference.java
More file actions
36 lines (34 loc) · 981 Bytes
/
MinimumAbsoluteDifference.java
File metadata and controls
36 lines (34 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package array_and_matrix;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* @Author: Wenhang Chen
* @Description:
* @Date: Created in 8:58 6/3/2020
* @Modified by:
*/
public class MinimumAbsoluteDifference {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
Arrays.sort(arr);
List<List<Integer>> res = new LinkedList<>();
int min = 99999999;
for (int i = 1; i < arr.length; i++) {
int tmp = arr[i] - arr[i - 1];
// 找到新的最小差,清空原有结果
if (tmp < min) {
min = tmp;
res.clear();
}
// 如果是最小差,记录
if (tmp == min) {
List<Integer> item = new ArrayList<>(2);
item.add(arr[i - 1]);
item.add(arr[i]);
res.add(item);
}
}
return res;
}
}