当前位置:网站首页>[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;
}
}
边栏推荐
- [cloud native] use of Nacos taskmanager task management
- Talking about fake demand from takeout order
- APICloud Studio3 API管理与调试使用教程
- LB10S-ASEMI整流桥LB10S
- 蜀天梦图×微言科技丨达梦图数据库朋友圈+1
- There is no monitoring and no operation and maintenance. The following is the commonly used script monitoring in monitoring
- go map
- JPA规范总结和整理
- “百度杯”CTF比赛 九月场,Web:Upload
- [deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
猜你喜欢

Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)

Principle and configuration of RSTP protocol

I'm doing open source in Didi

Lb10s-asemi rectifier bridge lb10s

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

Leetcode20. Valid parentheses

946. 验证栈序列

Fragmented knowledge management tool memos

国际自动机工程师学会(SAE International)战略投资几何伙伴

先写API文档还是先写代码?
随机推荐
函数传递参数小案例
##无监控,不运维,以下是监控里常用的脚本监控
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
SAE international strategic investment geometry partner
Android本地Sqlite数据库的备份和还原
[cloud native] use of Nacos taskmanager task management
[deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
How can non-technical departments participate in Devops?
Rocky基础知识1
“百度杯”CTF比赛 九月场,Web:Upload
How to protect user privacy without password authentication?
155. 最小栈
leetcode:221. 最大正方形【dp状态转移的精髓】
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
Talk about my drawing skills in my writing career
Principle and performance analysis of lepton lossless compression
It's too convenient. You can complete the code release and approval by nailing it!
SAP SEGW 事物码里的 ABAP Editor
Discussion on error messages and API versions of SAP ui5 getsaplogonlanguage is not a function