当前位置:网站首页>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;
}
}

边栏推荐
- Is it safe for China Galaxy Securities to open an account? Securities account opening
- 100million single men and women supported an IPO with a valuation of 13billion
- 【obs】libobs-winrt :CreateDispatcherQueueController
- Debezium系列之:IDEA集成词法和语法分析ANTLR,查看debezium支持的ddl、dml等语句
- Webuploader file upload drag upload progress monitoring type control upload result monitoring control
- PG基础篇--逻辑结构管理(用户及权限管理)
- 【无标题】
- 如何实现游戏中的在线计时器和离线计时器
- UWB超宽带定位技术,实时厘米级高精度定位应用,超宽带传输技术
- C# 语言的高级应用
猜你喜欢

Oracle fault handling: ora-10873:file * needs to be either taken out of backup or media recovered

The relationship between temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)

MMO項目學習一:預熱

面试官:Redis中集合数据类型的内部实现方式是什么?

建议收藏,我的腾讯Android面试经历分享

What is the core value of testing?

【AI 框架基础技术】自动求导机制 (Autograd)

Decision tree and random forest

Password reset of MariaDB root user and ordinary user

2022 the latest big company Android interview real problem analysis, Android development will be able to technology
随机推荐
MMO項目學習一:預熱
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*
PHP利用ueditor实现上传图片添加水印
The relationship between temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
Debezium系列之:postgresql从偏移量加载正确的最后一次提交 LSN
MMO project learning 1: preheating
国海证券在网上开户安全吗?
【AI 框架基础技术】自动求导机制 (Autograd)
Decision tree and random forest
What does software testing do? What are the requirements for learning?
Fuzor 2020 software installation package download and installation tutorial
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Webuploader file upload drag upload progress monitoring type control upload result monitoring control
太牛了,看这篇足矣了
Tutoriel de téléchargement et d'installation du progiciel fuzor 2020
How about testing outsourcing companies?
HAC cluster modifying administrator user password
JAD的安装、配置及集成IDEA
Hiengine: comparable to the local cloud native memory database engine