当前位置:网站首页>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;
}
}
边栏推荐
- 如何实现游戏中的在线计时器和离线计时器
- Oracle故障处理:Ora-10873:file * needs to be either taken out of backup or media recovered
- 通过POI追加数据到excel中小案例
- Xaas trap: all things serve (possible) is not what it really needs
- Summer Challenge database Xueba notes, quick review of exams / interviews~
- Reptile exercises (II)
- Go语言 | 03 数组、指针、切片用法
- Debezium系列之:修改源码支持unix_timestamp() as DEFAULT value
- C#应用程序界面开发基础——窗体控制(6)——菜单栏、工具栏和状态栏控件
- How about testing outsourcing companies?
猜你喜欢
5 years of experience, 27 days of Android programmer interview, 2022 programmer advanced classic
What do software test engineers do? How about the prospect of treatment?
HiEngine:可媲美本地的云原生内存数据库引擎
数学分析_笔记_第9章:曲线积分与曲面积分
众昂矿业:2022年全球萤石行业市场供给现状分析
PHP利用ueditor实现上传图片添加水印
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
Mysql如何对json数据进行查询及修改
随机推荐
中国银河证券开户安全吗 证券开户
#夏日挑战赛#数据库学霸笔记,考试/面试快速复习~
不愧是大佬,字节大牛耗时八个月又一力作
MMO项目学习一:预热
PG basics -- Logical Structure Management (user and permission management)
出海十年:新旧接力,黑马崛起
Notion 类生产力工具如何选择?Notion 、FlowUs 、Wolai 对比评测
PG基础篇--逻辑结构管理(用户及权限管理)
ELK分布式日志分析系统部署(华为云)
C#应用程序界面开发基础——窗体控制(6)——菜单栏、工具栏和状态栏控件
shell编程基础(第9篇:循环)
通过POI追加数据到excel中小案例
What do software test engineers do? How about the prospect of treatment?
四万字长文说operator new & operator delete
Fuzor 2020 software installation package download and installation tutorial
How to convert word into PDF? Word to PDF simple way to share!
什么是面上项目
建议收藏,我的腾讯Android面试经历分享
[performance test] jmeter+grafana+influxdb deployment practice
安卓面试宝典,2022Android面试笔试总结