当前位置:网站首页>力扣解法汇总1200-最小绝对差
力扣解法汇总1200-最小绝对差
2022-07-05 16:55:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给你个整数数组 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 <= 10^5
-10^6 <= arr[i] <= 10^6
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-absolute-difference
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 这题比较简单,排序之后,最小值一定是相邻的两者之间的最差值。
代码:
public class Solution1200 {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
Arrays.sort(arr);
List<List<Integer>> result = new ArrayList<>();
int minDIff = Integer.MAX_VALUE;
for (int i = 1; i < arr.length; i++) {
int diffValue = arr[i] - arr[i - 1];
if (diffValue < minDIff) {
result.clear();
result.add(createList(arr[i], arr[i - 1]));
minDIff = diffValue;
continue;
}
if (diffValue == minDIff) {
result.add(createList(arr[i], arr[i - 1]));
continue;
}
}
return result;
}
private List<Integer> createList(int i1, int i2) {
ArrayList<Integer> integers = new ArrayList<>();
integers.add(i2);
integers.add(i1);
return integers;
}
}
边栏推荐
- How to write a full score project document | acquisition technology
- 【testlink】TestLink1.9.18常见问题解决方法
- 【剑指 Offer】62. 圆圈中最后剩下的数字
- Design of electronic clock based on 51 single chip microcomputer
- Practical example of propeller easydl: automatic scratch recognition of industrial parts
- What are the precautions for MySQL group by
- VBA驱动SAP GUI实现办公自动化(二):判断元素是否存在
- 一个满分的项目文档是如何书写的|得物技术
- Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
- Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
猜你喜欢
Design of electronic clock based on 51 single chip microcomputer
基于Redis实现延时队列的优化方案小结
Etcd build a highly available etcd cluster
First day of learning C language
Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
Browser rendering principle and rearrangement and redrawing
激动人心!2022开放原子全球开源峰会报名火热开启!
7.Scala类
Judge whether a string is a full letter sentence
[Jianzhi offer] 63 Maximum profit of stock
随机推荐
普通程序员看代码,顶级程序员看趋势
一个满分的项目文档是如何书写的|得物技术
启牛商学院股票开户安全吗?靠谱吗?
How can C TCP set heartbeat packets to be elegant?
Machine learning compilation lesson 2: tensor program abstraction
flask解决CORS ERR 问题
Learn about MySQL transaction isolation level
EasyX second lesson
ThoughtWorks global CTO: build the architecture according to needs, and excessive engineering will only "waste people and money"
Matery主题自定义(一)黑夜模式
Deeply cultivate 5g, and smart core continues to promote 5g applications
C language to get program running time
WebApp开发-Google官方教程
[Web attack and Defense] WAF detection technology map
漫画:有趣的【海盗】问题
7. Scala class
Embedded-c language-6
【性能测试】jmeter+Grafana+influxdb部署实战
基于51单片机的电子时钟设计
[binary tree] insufficient nodes on the root to leaf path