当前位置:网站首页>Force buckle 1200 Minimum absolute difference
Force buckle 1200 Minimum absolute difference
2022-07-05 19:28:00 【Ruthless young Fisherman】
subject
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 .
Example
Input :arr = [4,2,1,3]
Output :[[1,2],[2,3],[3,4]]
Input :arr = [1,3,6,10,15]
Output :[[1,3]]
Input :arr = [3,8,-10,23,19,-4,-14,27]
Output :[[-14,-10],[19,23],[23,27]]
source : Power button (LeetCode)
link :https://leetcode.cn/problems/minimum-absolute-difference
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method 1: simulation
Java Realization
class Solution {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
int n = arr.length;
Arrays.sort(arr);
List<List<Integer>> res = new ArrayList<>();
int min = Integer.MAX_VALUE;
for (int i = 0; i < n - 1; i++) {
int cur = Math.abs(arr[i] - arr[i + 1]);
if (cur == min) {
res.add(Arrays.asList(arr[i], arr[i + 1]));
} else if (cur < min) {
min = cur;
res = new ArrayList<>();
res.add(Arrays.asList(arr[i], arr[i + 1]));
}
}
return res;
}
}
边栏推荐
- How to choose the notion productivity tools? Comparison and evaluation of notion, flowus and WOLAI
- ELK分布式日志分析系统部署(华为云)
- 完爆面试官,一线互联网企业高级Android工程师面试题大全
- Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
- 【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*
- 出海十年:新旧接力,黑马崛起
- Postman core function analysis - parameterization and test report
- 测试外包公司怎么样?
- Advanced application of C # language
- 测试的核心价值到底是什么?
猜你喜欢
Notion 类生产力工具如何选择?Notion 、FlowUs 、Wolai 对比评测
[performance test] jmeter+grafana+influxdb deployment practice
如何在2022年更明智地应用智能合约?
Xaas trap: all things serve (possible) is not what it really needs
Common interview questions in Android, 2022 golden nine silver ten Android factory interview questions hit
微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
Password reset of MariaDB root user and ordinary user
Teach you to deal with JS reverse picture camouflage hand in hand
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
面试官:Redis中集合数据类型的内部实现方式是什么?
随机推荐
The basic grammatical structure of C language
Oracle故障处理:Ora-10873:file * needs to be either taken out of backup or media recovered
#夏日挑战赛# HarmonyOS - 实现消息通知功能
Debezium系列之:修改源码支持drop foreign key if exists fk
打新债在哪里操作开户是更安全可靠的呢
Vagrant2.2.6 supports virtualbox6.1
XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
Advanced application of C # language
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
Microwave radar induction module technology, real-time intelligent detection of human existence, static micro motion and static perception
面试官:Redis中集合数据类型的内部实现方式是什么?
微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
How to convert word into PDF? Word to PDF simple way to share!
MySQL中字段类型为longtext的值导出后显示二进制串方式
2022 the latest big company Android interview real problem analysis, Android development will be able to technology
How about testing outsourcing companies?
爬虫练习题(二)