当前位置:网站首页>[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;
}
}
边栏推荐
- Concurrent performance test of SAP Spartacus with JMeter
- #yyds干货盘点# 解决名企真题:搬圆桌
- A detailed explanation of ASCII code, Unicode and UTF-8
- SAE international strategic investment geometry partner
- 手把手带你入门Apache伪静态的配置
- SAP UI5 ObjectPageLayout 控件使用方法分享
- 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]
- Laravel document reading notes -mews/captcha use (verification code function)
- Principle and performance analysis of lepton lossless compression
- Realize the addition of all numbers between 1 and number
猜你喜欢

蜀天梦图×微言科技丨达梦图数据库朋友圈+1

A specific example of ABAP type and EDM type mapping in SAP segw transaction code

山东大学暑期实训一20220620

Flutter draws animation effects of wave movement, curves and line graphs

How can non-technical departments participate in Devops?

逆波兰表达式

MySQL 巨坑:update 更新慎用影响行数做判断!!!

Lb10s-asemi rectifier bridge lb10s

STM32 and motor development (from architecture diagram to documentation)

Developers, is cloud native database the future?
随机推荐
Flutter InkWell & Ink组件
Rocky basic command 3
DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
SAP UI5 ObjectPageLayout 控件使用方法分享
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
Android本地Sqlite数据库的备份和还原
Concurrent performance test of SAP Spartacus with JMeter
go map
A detailed explanation of ASCII code, Unicode and UTF-8
使用Dom4j解析XML
CAN和CAN FD
[deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications
SAP ui5 objectpagelayout control usage sharing
leetcode 10. Regular Expression Matching 正则表达式匹配 (困难)
程序员成长第八篇:做好测试工作
Changing JS code has no effect
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
Go string operation
国际自动机工程师学会(SAE International)战略投资几何伙伴