当前位置:网站首页>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) 排序需要的空间
边栏推荐
- Flutter development: a way to solve the problem of blank space on the top of listview
- [system design] index monitoring and alarm system
- Getting started with Apache dolphin scheduler (one article is enough)
- Using directive in angualr2 to realize that the picture size changes with the window size
- Click the picture in the mobile browser and the picture will not pop up
- Oracle combines multiple rows of data into one row of data
- 90%的人都不懂的泛型,泛型的缺陷和应用场景
- From "chemist" to developer, from Oracle to tdengine, two important choices in my life
- [NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
- 面试:List 如何根据对象的属性去重?
猜你喜欢

On July 2, I invite you to TD Hero online press conference

RMS TO EAP通过MQTT简单实现

Pagoda panel MySQL cannot be started

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

Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 -下

【OpenCV 例程200篇】219. 添加数字水印(盲水印)

Solve the problem of no all pattern found during Navicat activation and registration

Evolution of Baidu intelligent applet patrol scheduling scheme

Kotlin compose multiple item scrolling

@SerializedName注解使用
随机推荐
Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常
Single chip microcomputer principle and Interface Technology (esp8266/esp32) machine human draft
Small program startup performance optimization practice
[app packaging error] to proceed, either fix the issues identified by lint, or modify your build script as follow
Coordinate system of view
宝塔面板MySQL无法启动
Jupiter notebook shortcut key
Tdengine offline upgrade process
From "chemist" to developer, from Oracle to tdengine, two important choices in my life
【系统设计】指标监控和告警系统
[tips] get the x-axis and y-axis values of cdfplot function in MATLAB
View Slide
Mysql80 service does not start
Kotlin compose and native nesting
Are databases more popular as they get older?
天龙八部TLBB系列 - 关于包裹掉落的物品
Personal website construction tutorial | local website environment construction | website production tutorial
苹果 5G 芯片研发失败?想要摆脱高通为时过早
Tianlong Babu TLBB series - about items dropped from packages
Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)