当前位置:网站首页>leetcode:1200. 最小绝对差
leetcode:1200. 最小绝对差
2022-07-05 09:43:00 【uncle_ll】
1200. 最小绝对差
来源:力扣(LeetCode)
链接: https://leetcode.cn/problems/minimum-absolute-difference
给你个整数数组 arr,其中每个元素都 不相同。
请你找到所有具有最小绝对差的元素对,并且按升序的顺序返回。
示例 1:
输入:arr = [4,2,1,3]
输出:[[1,2],[2,3],[3,4]]
示例 2:
输入:arr = [1,3,6,10,15]
输出:[[1,3]]
示例 3:
输入:arr = [3,8,-10,23,19,-4,-14,27]
输出:[[-14,-10],[19,23],[23,27]]
提示:
- 2 <= arr.length <= 1 0 5 10^5 105
- − 1 0 6 -10^6 −106 <= arr[i] <= 1 0 6 10^6 106
解法
- 排序+遍历: 排序后相邻元素的差就是该元素的最小的绝对差,排序之后进行遍历,如果绝对差小于最小绝对差,更新绝对差,更新结果;如果等于最小绝对差,结果中增加一个结果即可;大于的话不进行处理;
代码实现
排序+遍历
python实现
class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
n = len(arr)
res = []
arr.sort()
min_diff = float('inf')
for i in range(1, n):
if arr[i] - arr[i-1] < min_diff:
res = [[arr[i-1], arr[i]]]
min_diff = arr[i] - arr[i-1]
elif arr[i] - arr[i-1] == min_diff:
res.append([arr[i-1], arr[i]])
return res
c++实现
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
int n = arr.size();
vector<vector<int>> res;
sort(arr.begin(), arr.end());
int min_diff = INT_MAX;
for (int i=1; i<n; i++) {
if (arr[i]-arr[i-1] < min_diff){
res = {
{
arr[i-1], arr[i]}};
min_diff = arr[i] - arr[i-1];
}
else if (arr[i]-arr[i-1] == min_diff)
res.push_back({
arr[i-1], arr[i]});
}
return res;
}
};
复杂度分析
- 时间复杂度: O ( N l o g N ) O(NlogN) O(NlogN) 排序耗时
- 空间复杂度: O ( l o g N ) O(logN) O(logN) 排序需要的空间
边栏推荐
- ArcGIS Pro 创建要素
- 【C语言】动态内存开辟的使用『malloc』
- Android SQLite database encryption
- The Alipay in place function can't be found, and the Alipay in place function is offline
- Openes version query
- Z-blog template installation and use tutorial
- Optimize database queries using the cursor object of SQLite
- How Windows bat script automatically executes sqlcipher command
- [technical live broadcast] how to rewrite tdengine code from 0 to 1 with vscode
- [tips] get the x-axis and y-axis values of cdfplot function in MATLAB
猜你喜欢

H. 265 introduction to coding principles

ArcGIS Pro creating features

How to use sqlcipher tool to decrypt encrypted database under Windows system

Application of data modeling based on wide table

From "chemist" to developer, from Oracle to tdengine, two important choices in my life

Uncover the practice of Baidu intelligent testing in the field of automatic test execution

《微信小程序-基础篇》小程序中的事件与冒泡

Kotlin compose multiple item scrolling

Small program startup performance optimization practice

一种用于干式脑电图的高密度256通道电极帽
随机推荐
CSDN always jumps to other positions when editing articles_ CSDN sends articles without moving the mouse
C#函数返回多个值方法
Click the picture in the mobile browser and the picture will not pop up
[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
Tongweb set gzip
From "chemist" to developer, from Oracle to tdengine, two important choices in my life
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条...
Comment obtenir le temps STW du GC (collecteur d'ordures)?
高级 OpenCV:BGR 像素强度图
天龙八部TLBB系列 - 关于包裹掉落的物品
最全是一次I2C总结
Baidu app's continuous integration practice based on pipeline as code
[200 opencv routines] 219 Add digital watermark (blind watermark)
@SerializedName注解使用
The comparison of every() and some() in JS uses a power storage plan
MySQL character type learning notes
Personal website construction tutorial | local website environment construction | website production tutorial
Advanced opencv:bgr pixel intensity map
天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题
盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名