当前位置:网站首页>[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;
}
}
边栏推荐
- AVC1与H264的区别
- 爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
- LeetCode20.有效的括号
- UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
- leetcode:221. 最大正方形【dp状态转移的精髓】
- 山东大学暑期实训一20220620
- ##无监控,不运维,以下是监控里常用的脚本监控
- DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
- Run, open circuit
- 数据湖(七):Iceberg概念及回顾什么是数据湖
猜你喜欢
山东大学暑期实训一20220620
《2022年中國銀行業RPA供應商實力矩陣分析》研究報告正式啟動
Although the volume and price fall, why are the structural deposits of commercial banks favored by listed companies?
LeetCode20.有效的括号
Pycharm installation third party library diagram
将函数放在模块中
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
数据泄露怎么办?'华生·K'7招消灭安全威胁
Le rapport de recherche sur l'analyse matricielle de la Force des fournisseurs de RPA dans le secteur bancaire chinois en 2022 a été officiellement lancé.
Association modeling method in SAP segw transaction code
随机推荐
数据泄露怎么办?'华生·K'7招消灭安全威胁
C# 对象存储
go 数组与切片
Yyds dry inventory JS intercept file suffix
Association modeling method in SAP segw transaction code
Pycharm installation third party library diagram
百度杯”CTF比赛 2017 二月场,Web:爆破-2
Laravel document reading notes -mews/captcha use (verification code function)
Developers, is cloud native database the future?
go map
同事半个月都没搞懂selenium,我半个小时就给他整明白!顺手秀了一波爬淘宝的操作[通俗易懂]
It's too convenient. You can complete the code release and approval by nailing it!
A specific example of ABAP type and EDM type mapping in SAP segw transaction code
SAE international strategic investment geometry partner
[深度学习论文笔记]TransBTSV2: Wider Instead of Deeper Transformer for Medical Image Segmentation
Write macro with word
#从源头解决# 自定义头文件在VS上出现“无法打开源文件“XX.h“的问题
【Hot100】33. 搜索旋转排序数组
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
函数传递参数小案例