当前位置:网站首页>[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的区别
- Put functions in modules
- 碎片化知识管理工具Memos
- How to protect user privacy without password authentication?
- go 字符串操作
- MySQL --- 数据库查询 - 排序查询、分页查询
- The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)
- Hiengine: comparable to the local cloud native memory database engine
- leetcode:221. Maximum square [essence of DP state transition]
- I'm doing open source in Didi
猜你喜欢
Word document injection (tracking word documents) incomplete
"Baidu Cup" CTF competition in September, web:sql
《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
潘多拉 IOT 开发板学习(HAL 库)—— 实验7 窗口看门狗实验(学习笔记)
OpenHarmony应用开发之Navigation组件详解
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
SAP UI5 DynamicPage 控件介绍
Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1
Reverse Polish notation
百度杯”CTF比赛 2017 二月场,Web:爆破-2
随机推荐
Although the volume and price fall, why are the structural deposits of commercial banks favored by listed companies?
关于 Notion-Like 工具的反思和畅想
手把手带你入门Apache伪静态的配置
LB10S-ASEMI整流桥LB10S
无密码身份验证如何保障用户隐私安全?
从外卖点单浅谈伪需求
Pandora IOT development board learning (HAL Library) - Experiment 7 window watchdog experiment (learning notes)
阿里云SLB负载均衡产品基本概念与购买流程
MSTP and eth trunk
爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
程序员成长第八篇:做好测试工作
Developers, is cloud native database the future?
Android本地Sqlite数据库的备份和还原
SAP UI5 ObjectPageLayout 控件使用方法分享
APICloud Studio3 API管理与调试使用教程
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
峰会回顾|保旺达-合规和安全双驱动的数据安全整体防护体系
[深度学习论文笔记]TransBTSV2: Wider Instead of Deeper Transformer for Medical Image Segmentation
Get to know linkerd project for the first time
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]