当前位置:网站首页>【每日一题】1200. 最小绝对差
【每日一题】1200. 最小绝对差
2022-07-05 12:56:00 【王六六的IT日常】
1200. 最小绝对差
简单题
给你个整数数组 arr,其中每个元素都 不相同。
请你找到所有具有最小绝对差的元素对,并且按升序的顺序返回。

排序+模拟:
对 arr 进行排序,容易得知差值最小值必然发生在排序数组的相邻元素之间,此时我们可以通过遍历排序数组并使用变量 min 记录当前差值最小值来统计答案。
class Solution {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
Arrays.sort(arr);
List<List<Integer>> ans = new ArrayList<>();
int n = arr.length, min = arr[1] - arr[0];
for (int i = 0; i < n - 1; i++) {
int cur = arr[i + 1] - arr[i];
if (cur < min) {
ans.clear();
min = cur;
}
if (cur == min) {
List<Integer> temp = new ArrayList<>();
temp.add(arr[i]);
temp.add(arr[i + 1]);
ans.add(temp);
}
}
return ans;
}
}
边栏推荐
- mysql拆分字符串做条件查询
- uni-app开发语音识别app,讲究的就是简单快速。
- 从外卖点单浅谈伪需求
- Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function
- [cloud native] use of Nacos taskmanager task management
- 精彩速递|腾讯云数据库6月刊
- Yyds dry goods inventory # solve the real problem of famous enterprises: move the round table
- Write macro with word
- 关于 SAP UI5 getSAPLogonLanguage is not a function 的错误消息以及 API 版本的讨论
- RHCSA8
猜你喜欢

Principle and configuration of RSTP protocol

Concurrent performance test of SAP Spartacus with JMeter

【服务器数据恢复】某品牌服务器存储raid5数据恢复案例

关于 SAP UI5 floating footer 显示与否的单步调试以及使用 SAP UI5 的收益

SAP UI5 视图里的 OverflowToolbar 控件

Pandora IOT development board learning (HAL Library) - Experiment 7 window watchdog experiment (learning notes)

Lepton 无损压缩原理及性能分析

Actual combat simulation │ JWT login authentication

How to realize batch sending when fishing

SAP ui5 objectpagelayout control usage sharing
随机推荐
LB10S-ASEMI整流桥LB10S
155. Minimum stack
Pandora IOT development board learning (HAL Library) - Experiment 7 window watchdog experiment (learning notes)
百日完成国产数据库opengausss的开源任务--openGuass极简版3.0.0安装教程
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
RHCSA9
Introduction to the principle of DNS
Yyds dry inventory JS intercept file suffix
Lepton 无损压缩原理及性能分析
leetcode:221. Maximum square [essence of DP state transition]
逆波兰表达式
Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function
MySQL 巨坑:update 更新慎用影响行数做判断!!!
关于 SAP UI5 floating footer 显示与否的单步调试以及使用 SAP UI5 的收益
SAP SEGW 事物码里的 Association 建模方式
开发者,云原生数据库是未来吗?
Association modeling method in SAP segw transaction code
SAP SEGW 事物码里的 ABAP 类型和 EDM 类型映射的一个具体例子
MSTP and eth trunk
碎片化知识管理工具Memos