当前位置:网站首页>[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;
}
}
边栏推荐
- Pycharm installation third party library diagram
- ##无监控,不运维,以下是监控里常用的脚本监控
- [深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
- Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications
- 946. Verify stack sequence
- Navigation property and entityset usage in SAP segw transaction code
- CAN和CAN FD
- Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
- Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
- Simple page request and parsing cases
猜你喜欢

MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!

Didi open source Delta: AI developers can easily train natural language models

OpenHarmony应用开发之Navigation组件详解

uni-app开发语音识别app,讲究的就是简单快速。

MSTP and eth trunk

Reverse Polish notation

Small case of function transfer parameters

百度杯”CTF比赛 2017 二月场,Web:爆破-2
![leetcode:221. Maximum square [essence of DP state transition]](/img/ea/158e8659657984794c52a0449e0ee5.png)
leetcode:221. Maximum square [essence of DP state transition]

量价虽降,商业银行结构性存款为何受上市公司所偏爱?
随机推荐
LeetCode20.有效的括号
My colleague didn't understand selenium for half a month, so I figured it out for him in half an hour! Easily showed a wave of operations of climbing Taobao [easy to understand]
数据泄露怎么办?'华生·K'7招消灭安全威胁
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
精彩速递|腾讯云数据库6月刊
Natural language processing series (I) introduction overview
Halcon template matching actual code (I)
go map
MATLAB论文图表标准格式输出(干货)
Detailed explanation of navigation component of openharmony application development
MySQL 巨坑:update 更新慎用影响行数做判断!!!
js判断数组中是否存在某个元素(四种方法)
Talk about seven ways to realize asynchronous programming
手把手带你入门Apache伪静态的配置
CF:A. The Third Three Number Problem【关于我是位运算垃圾这个事情】
将函数放在模块中
AVC1与H264的区别
初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。
A specific example of ABAP type and EDM type mapping in SAP segw transaction code
Binder通信过程及ServiceManager创建过程