当前位置:网站首页>【日常训练】1200. 最小绝对差
【日常训练】1200. 最小绝对差
2022-07-05 08:36:00 【Puppet__】
题目
给你个整数数组 arr,其中每个元素都 不相同。
请你找到所有具有最小绝对差的元素对,并且按升序的顺序返回。
示例 1:
输入:arr = [4,2,1,3]
输出:[[1,2],[2,3],[3,4]]
示例 2:
输入:arr = [1,3,6,10,15]
输出:[[1,3]]
示例 3:
输入:arr = [3,8,-10,23,19,-4,-14,27]
输出:[[-14,-10],[19,23],[23,27]]
提示:
2 <= arr.length <= 105
-106 <= arr[i] <= 106
代码
package dayLeetCode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class dayleetcode1200 {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
Arrays.sort(arr);
List<List<Integer>> ansList = new ArrayList<>();
int maxCnt = Integer.MAX_VALUE;
for (int i = 0; i < arr.length - 1; i++){
int tmp = arr[i + 1] - arr[i];
maxCnt = Math.min(tmp, maxCnt);
}
for (int i = 0; i < arr.length - 1; i++){
int tmp = arr[i + 1] - arr[i];
if (tmp == maxCnt){
List<Integer> tmpList = new ArrayList<>();
tmpList.add(arr[i]);
tmpList.add(arr[i + 1]);
ansList.add(tmpList);
}
}
return ansList;
}
public static void main(String[] args) {
dayleetcode1200 obj = new dayleetcode1200();
System.out.println(obj.minimumAbsDifference(new int[]{
4, 2, 1, 3}));
}
}
边栏推荐
- Sword finger offer 06 Print linked list from end to end
- Several important parameters of LDO circuit design and type selection
- Go dependency injection -- Google open source library wire
- 实例009:暂停一秒输出
- 猜谜语啦(10)
- [three tier architecture]
- One question per day - replace spaces
- 猜谜语啦(6)
- 剑指 Offer 09. 用两个栈实现队列
- STM32 --- NVIC interrupt
猜你喜欢
随机推荐
剑指 Offer 09. 用两个栈实现队列
Business modeling of software model | stakeholders
Xrosstools tool installation for X-Series
Installation and use of libjpeg and ligpng
Array integration initialization (C language)
Cmder of win artifact
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
Example 009: pause output for one second
轮子1:QCustomPlot初始化模板
QEMU demo makefile analysis
猜谜语啦(8)
Guess riddles (3)
Arrangement of some library files
MySQL之MHA高可用集群
關於線性穩壓器的五個設計細節
实例009:暂停一秒输出
Sword finger offer 09 Implementing queues with two stacks
[three tier architecture and JDBC summary]
Wheel 1:qcustomplot initialization template
猜谜语啦(5)








