当前位置:网站首页>[daily question] 1200 Minimum absolute difference
[daily question] 1200 Minimum absolute difference
2022-07-05 13:14:00 【Wang Liuliu's it daily】
1200. Minimum absolute difference
Simple questions
Here's an array of integers arr, Each of these elements is inequality .
Please find all the elements with the least absolute difference , And return in ascending order .

Sort + simulation :
Yes arr Sort , It is easy to know that the minimum value of the difference must occur in the sorting array adjacent Between elements , At this point, we can sort the array by traversing and using variables min Record the minimum value of the current difference to count the answers .
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;
}
}
边栏推荐
- Overflow toolbar control in SAP ui5 view
- PyCharm安装第三方库图解
- 事务的基本特性和隔离级别
- SAP ui5 objectpagelayout control usage sharing
- 无密码身份验证如何保障用户隐私安全?
- [深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2
- 946. 验证栈序列
- 碎片化知识管理工具Memos
- Rocky基础知识1
- [notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
猜你喜欢

量价虽降,商业银行结构性存款为何受上市公司所偏爱?

Simple page request and parsing cases

LB10S-ASEMI整流桥LB10S

解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107

逆波兰表达式

LeetCode20.有效的括号

PyCharm安装第三方库图解

数据湖(七):Iceberg概念及回顾什么是数据湖

Introduction to sap ui5 flexiblecolumnlayout control

Lb10s-asemi rectifier bridge lb10s
随机推荐
程序员成长第八篇:做好测试工作
LB10S-ASEMI整流桥LB10S
同事半个月都没搞懂selenium,我半个小时就给他整明白!顺手秀了一波爬淘宝的操作[通俗易懂]
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
LeetCode20.有效的括号
百日完成国产数据库opengausss的开源任务--openGuass极简版3.0.0安装教程
Yyds dry goods inventory # solve the real problem of famous enterprises: move the round table
Overflow toolbar control in SAP ui5 view
A specific example of ABAP type and EDM type mapping in SAP segw transaction code
[deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
Talk about seven ways to realize asynchronous programming
go map
JPA规范总结和整理
先写API文档还是先写代码?
Difference between avc1 and H264
SAE international strategic investment geometry partner
I'm doing open source in Didi
Pycharm installation third party library diagram
go map
《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动