当前位置:网站首页>力扣解法汇总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;
}
}边栏推荐
- 兰空图床苹果快捷指令
- C (WinForm) the current thread is not in a single threaded unit, so ActiveX controls cannot be instantiated
- Oracle缩表空间的完整解决实例
- Wsl2.0 installation
- goto Statement
- WebApp开发-Google官方教程
- [Jianzhi offer] 62 The last remaining number in the circle
- Winedt common shortcut key modify shortcut key latex compile button
- Embedded UC (UNIX System Advanced Programming) -2
- Learn about MySQL transaction isolation level
猜你喜欢

深耕5G,芯讯通持续推动5G应用百花齐放

Machine learning 02: model evaluation

IDC报告:腾讯云数据库稳居关系型数据库市场TOP 2!

机器学习02:模型评估

Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition

Error in composer installation: no composer lock file present.

激动人心!2022开放原子全球开源峰会报名火热开启!
MYSQL group by 有哪些注意事项

7.Scala类

ternary operator
随机推荐
【beanshell】数据写入本地多种方法
精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
[wechat applet] read the life cycle and route jump of the applet
一文了解MySQL事务隔离级别
Domain name resolution, reverse domain name resolution nbtstat
Embedded-c Language-3
Tips for extracting JSON fields from MySQL
关于mysql中的json解析函数JSON_EXTRACT
通过proc接口调试内核代码
干货!半监督预训练对话模型 SPACE
Application of threshold homomorphic encryption in privacy Computing: Interpretation
thinkphp3.2.3
C#实现水晶报表绑定数据并实现打印3-二维码条形码
Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.
忽米沄析:工业互联网标识解析与企业信息系统的融合应用
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
Read the basic grammar of C language in one article
基于Redis实现延时队列的优化方案小结
thinkphp3.2.3
Browser rendering principle and rearrangement and redrawing