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

边栏推荐
- 【AI 框架基础技术】自动求导机制 (Autograd)
- 【C语言】字符串函数及模拟实现strlen&&strcpy&&strcat&&strcmp
- The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
- 5 years of experience, 27 days of Android programmer interview, 2022 programmer advanced classic
- MMO项目学习一:预热
- Mysql如何对json数据进行查询及修改
- [AI framework basic technology] automatic derivation mechanism (autograd)
- Mariadb root用户及普通用户的密码 重置
- 国海证券在网上开户安全吗?
- Advanced application of C # language
猜你喜欢

四万字长文说operator new & operator delete

5 years of experience, 27 days of Android programmer interview, 2022 programmer advanced classic
The problem of returning the longtext field in MySQL and its solution

C# 语言的基本语法结构

webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件

UWB超宽带定位技术,实时厘米级高精度定位应用,超宽带传输技术

How to convert word into PDF? Word to PDF simple way to share!

如何实现游戏中的在线计时器和离线计时器

Common interview questions in Android, 2022 golden nine silver ten Android factory interview questions hit

软件测试是干什么的?学习有啥要求?
随机推荐
从零实现深度学习框架——LSTM从理论到实战【实战】
#夏日挑战赛#数据库学霸笔记,考试/面试快速复习~
C# 语言的高级应用
14、用户、组和权限(14)
测试的核心价值到底是什么?
How about testing outsourcing companies?
Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
司空见惯 - 英雄扫雷鼠
Common - Hero Minesweeper
Blue sky drawing bed Apple quick instructions
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
IBM大面积辞退40岁+的员工,掌握这十个搜索技巧让你的工作效率至上提高十倍
【硬核干货】数据分析哪家强?选Pandas还是选SQL
JS解力扣每日一题(十二)——556. 下一个更大元素 III(2022-7-3)
Postman core function analysis - parameterization and test report
使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
Information / data
Notion 类生产力工具如何选择?Notion 、FlowUs 、Wolai 对比评测