import java.util.*;
/**
* @author xnl
* @Description:
* @date: 2022/7/4 21:35
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
int[] arr = {1,3,6,10,15};
System.out.println(solution.minimumAbsDifference(arr));
}
public List<List<Integer>> minimumAbsDifference(int[] arr) {
Arrays.sort(arr);
List<List<Integer>> res = new ArrayList<>();
// 使用栈来存储
Deque<List<Integer>> deque = new LinkedList<>();
deque.offer(Arrays.asList(new Integer[]{arr[0], arr[1]}));
for (int i = 2; i < arr.length; i++){
int x = arr[i] - arr[i - 1];
int y = deque.peek().get(1) - deque.peek().get(0);
if (x > y){
continue;
}
if (x > y){
while (!deque.isEmpty()){
deque.pop();
}
}
deque.offer(Arrays.asList(new Integer[]{arr[i - 1], arr[i]}));
}
while (!deque.isEmpty()){
res.add(deque.poll());
}
return res;
}
}

哈哈哈
本文介绍了一种寻找整数数组中具有最小绝对差值的所有连续元素对的方法。通过排序数组并迭代一次,利用栈结构存储可能的候选答案,最终得到具有最小绝对差值的元素对列表。
216

被折叠的 条评论
为什么被折叠?



