当前位置:网站首页>[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;
}
}
边栏推荐
- 同事半个月都没搞懂selenium,我半个小时就给他整明白!顺手秀了一波爬淘宝的操作[通俗易懂]
- 《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
- “百度杯”CTF比赛 九月场,Web:Upload
- FPGA 学习笔记:Vivado 2019.1 添加 IP MicroBlaze
- Introduction to sap ui5 flexiblecolumnlayout control
- I'm doing open source in Didi
- uni-app开发语音识别app,讲究的就是简单快速。
- DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
- [notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
- 山东大学暑期实训一20220620
猜你喜欢
简单上手的页面请求和解析案例
百度杯”CTF比赛 2017 二月场,Web:爆破-2
About the single step debugging of whether SAP ui5 floating footer is displayed or not and the benefits of using SAP ui5
数据湖(七):Iceberg概念及回顾什么是数据湖
Overflow toolbar control in SAP ui5 view
Write macro with word
Alibaba cloud SLB load balancing product basic concept and purchase process
OpenHarmony应用开发之Navigation组件详解
C# 对象存储
Lb10s-asemi rectifier bridge lb10s
随机推荐
函数的默认参数&函数参数的多种方法
RHCSA9
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
RHCSA8
Notion 类笔记软件如何选择?Notion 、FlowUs 、Wolai 对比评测
"Baidu Cup" CTF competition in September, web:sql
Although the volume and price fall, why are the structural deposits of commercial banks favored by listed companies?
Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
Natural language processing series (I) introduction overview
ABAP editor in SAP segw transaction code
C# 对象存储
SAE international strategic investment geometry partner
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
Talking about fake demand from takeout order
How to protect user privacy without password authentication?
OpenHarmony应用开发之Navigation组件详解
Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
碎片化知识管理工具Memos
CloudCompare——点云切片
It's too convenient. You can complete the code release and approval by nailing it!