当前位置:网站首页>力扣 1200. 最小绝对差
力扣 1200. 最小绝对差
2022-07-05 19:18:00 【冷酷的摸鱼小将】
题目
给你个整数数组 arr,其中每个元素都 不相同。
请你找到所有具有最小绝对差的元素对,并且按升序的顺序返回。
示例
输入:arr = [4,2,1,3]
输出:[[1,2],[2,3],[3,4]]
输入:arr = [1,3,6,10,15]
输出:[[1,3]]
输入:arr = [3,8,-10,23,19,-4,-14,27]
输出:[[-14,-10],[19,23],[23,27]]
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-absolute-difference
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
方法1:模拟
Java实现
class Solution {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
int n = arr.length;
Arrays.sort(arr);
List<List<Integer>> res = new ArrayList<>();
int min = Integer.MAX_VALUE;
for (int i = 0; i < n - 1; i++) {
int cur = Math.abs(arr[i] - arr[i + 1]);
if (cur == min) {
res.add(Arrays.asList(arr[i], arr[i + 1]));
} else if (cur < min) {
min = cur;
res = new ArrayList<>();
res.add(Arrays.asList(arr[i], arr[i + 1]));
}
}
return res;
}
}
边栏推荐
- HAC cluster modifying administrator user password
- #夏日挑战赛# HarmonyOS - 实现消息通知功能
- 爬虫练习题(二)
- 微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
- word如何转换成pdf?word转pdf简单的方法分享!
- 国内低代码开发平台靠谱的都有哪些?
- The relationship between temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
- 全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
- How to convert word into PDF? Word to PDF simple way to share!
- Fuzor 2020軟件安裝包下載及安裝教程
猜你喜欢
Common - Hero Minesweeper
公司破产后,黑石们来了
C# 语言的基本语法结构
不愧是大佬,字节大牛耗时八个月又一力作
强化学习-学习笔记4 | Actor-Critic
Go deep into the underlying C source code and explain the core design principles of redis
面试官:Redis中集合数据类型的内部实现方式是什么?
How to convert word into PDF? Word to PDF simple way to share!
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
Fuzor 2020软件安装包下载及安装教程
随机推荐
Tutoriel de téléchargement et d'installation du progiciel fuzor 2020
Debezium系列之:解析默认值字符集
Decision tree and random forest
开源 SPL 消灭数以万计的数据库中间表
uniapp获取微信头像和昵称
Is it safe for Guohai Securities to open an account online?
集合
How to choose the notion productivity tools? Comparison and evaluation of notion, flowus and WOLAI
How to quickly advance automated testing? Listen to the personal feelings of the three bat test engineers
Debezium系列之:记录mariadb数据库删除多张临时表debezium解析到的消息以及解决方法
华为让出的高端市场,小米12S靠徕卡能抢到吗?
How to realize the Online timer and offline timer in the game
软件测试是干什么的?学习有啥要求?
Tupu software digital twin | visual management system based on BIM Technology
软件测试工程师是做什么的?待遇前景怎么样?
What does software testing do? What are the requirements for learning?
Which securities company is better and which platform is safer for mobile account opening
Analysis of postman core functions - parameterization and test report
100million single men and women supported an IPO with a valuation of 13billion
毫米波雷达人体感应器,智能感知静止存在,人体存在检测应用