标签:https bsd solution val new t tps java 描述 code
第二题尝试了两种解法,一种超时间、一种超内存,只AC了一题。归根结底还是太菜。
数组排好序,获取最小的差值即可。
public class Solution {
class Test {
public List<Integer> list;
public Integer num;
}
public List<List<Integer>> minimumAbsDifference(int[] arr) {
int len = arr.length;
List<Test> testlist = new ArrayList<>();
List<List<Integer>> res = new ArrayList<>();
//数组排好序
Arrays.sort(arr);
int last = arr[0];
int minRes = Integer.MAX_VALUE;
for (int i = 1; i < len; i++) {
List<Integer> list = new ArrayList<>();
list.add(last);
list.add(arr[i]);
Test test = new Test();
test.list = list;
test.num = arr[i] - last;
//获取最小差值
minRes = test.num < minRes ? test.num : minRes;
testlist.add(test);
last = arr[i];
}
//取出等于最小差值的数组
for (Test test : testlist) {
if (test.num == minRes) {
res.add(test.list);
}
}
return res;
}
}
标签:https bsd solution val new t tps java 描述 code
原文地址:https://www.cnblogs.com/fonxian/p/11566933.html