当前位置:网站首页>【日常训练】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}));
}
}
边栏推荐
- [paper reading] the latest transfer ability in deep learning: a survey in 2022
- 实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
- 暑假第一周
- Working principle and type selection of common mode inductor
- Five design details of linear regulator
- Run菜单解析
- MySQL MHA high availability cluster
- L298N module use
- Is the security account given by Yixue school safe? Where can I open an account
- 猜谜语啦(142)
猜你喜欢
实例007:copy 将一个列表的数据复制到另一个列表中。
Simple design description of MIC circuit of ECM mobile phone
Example 006: Fibonacci series
MySQL之MHA高可用集群
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
Installation and use of libjpeg and ligpng
Take you to understand the working principle of lithium battery protection board
Sword finger offer 06 Print linked list from end to end
How to write cover letter?
99 multiplication table (C language)
随机推荐
Take you to understand the working principle of lithium battery protection board
STM32---IIC
STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
STM32 single chip microcomputer - external interrupt
How can fresh students write resumes to attract HR and interviewers
Example 007: copy data from one list to another list.
On boost circuit
TypeScript手把手教程,简单易懂
剑指 Offer 06. 从尾到头打印链表
Briefly talk about the identification protocol of mobile port -bc1.2
Business modeling of software model | stakeholders
leetcode - 445. 两数相加 II
MySQL之MHA高可用集群
C language data type replacement
GEO数据库中搜索数据
Business modeling of software model | vision
Cinq détails de conception du régulateur de tension linéaire
Guess riddles (2)
猜谜语啦(10)
STM32 single chip microcomputer -- volatile keyword