当前位置:网站首页>【每日一题】1200. 最小绝对差
【每日一题】1200. 最小绝对差
2022-07-05 12:56:00 【王六六的IT日常】
1200. 最小绝对差
简单题
给你个整数数组 arr,其中每个元素都 不相同。
请你找到所有具有最小绝对差的元素对,并且按升序的顺序返回。

排序+模拟:
对 arr 进行排序,容易得知差值最小值必然发生在排序数组的相邻元素之间,此时我们可以通过遍历排序数组并使用变量 min 记录当前差值最小值来统计答案。
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;
}
}
边栏推荐
- Put functions in modules
- MSTP and eth trunk
- 量价虽降,商业银行结构性存款为何受上市公司所偏爱?
- #yyds干货盘点# 解决名企真题:搬圆桌
- RHCSA1
- It's too convenient. You can complete the code release and approval by nailing it!
- SAP UI5 DynamicPage 控件介绍
- Notion 类笔记软件如何选择?Notion 、FlowUs 、Wolai 对比评测
- How can non-technical departments participate in Devops?
- Taobao short video, why the worse the effect
猜你喜欢

A specific example of ABAP type and EDM type mapping in SAP segw transaction code

精彩速递|腾讯云数据库6月刊

Concurrent performance test of SAP Spartacus with JMeter

Reverse Polish notation

UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt

Flutter 绘制波浪移动动画效果,曲线和折线图

国际自动机工程师学会(SAE International)战略投资几何伙伴

无密码身份验证如何保障用户隐私安全?

Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)

RHCSA3
随机推荐
155. 最小栈
CF:A. The Third Three Number Problem【关于我是位运算垃圾这个事情】
《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
Flutter 绘制波浪移动动画效果,曲线和折线图
RHCSA4
HiEngine:可媲美本地的云原生内存数据库引擎
关于 Notion-Like 工具的反思和畅想
ABAP editor in SAP segw transaction code
百日完成国产数据库opengausss的开源任务--openGuass极简版3.0.0安装教程
Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1
使用Dom4j解析XML
Introduction to sap ui5 flexiblecolumnlayout control
关于 SAP UI5 floating footer 显示与否的单步调试以及使用 SAP UI5 的收益
STM32 and motor development (from architecture diagram to documentation)
Didi open source Delta: AI developers can easily train natural language models
Yyds dry inventory JS intercept file suffix
A specific example of ABAP type and EDM type mapping in SAP segw transaction code
JPA规范总结和整理
Rocky基础知识1
#从源头解决# 自定义头文件在VS上出现“无法打开源文件“XX.h“的问题