当前位置:网站首页>【日常训练】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}));
}
}
边栏推荐
- Search data in geo database
- MATLAB小技巧(28)模糊綜合評價
- go依赖注入--google开源库wire
- Business modeling of software model | stakeholders
- Xrosstools tool installation for X-Series
- 猜谜语啦(7)
- Weidongshan Internet of things learning lesson 1
- Business modeling of software model | object modeling
- 如何写Cover Letter?
- Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
猜你喜欢

实例010:给人看的时间

猜谜语啦(3)

Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off

An enterprise information integration system

猜谜语啦(11)
![[three tier architecture and JDBC summary]](/img/e0/13d48f2e59b445b9e28e38d45f492d.png)
[three tier architecture and JDBC summary]

STM32 single chip microcomputer -- volatile keyword

Old Wang's esp8266 and old Wu's ws2818 light strip

Run菜单解析

如何写Cover Letter?
随机推荐
Guess riddles (7)
287. 寻找重复数-快慢指针
Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
One question per day - replace spaces
猜谜语啦(8)
图解八道经典指针笔试题
猜谜语啦(7)
亿学学堂给的证券账户安不安全?哪里可以开户
Installation and use of libjpeg and ligpng
L298N module use
STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
STM32 --- GPIO configuration & GPIO related library functions
STM32 lights up the 1.8-inch screen under Arduino IDE
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
Arrangement of some library files
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
696. 计数二进制子串
剑指 Offer 05. 替换空格
Arduino operation stm32