当前位置:网站首页>leetcode-1200:最小绝对差
leetcode-1200:最小绝对差
2022-07-05 05:46:00 【菊头蝙蝠】
leetcode-1200:最小绝对差
题目
给你个整数数组 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]]
解题
方法一:排序+一次遍历
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
sort(arr.begin(),arr.end());
vector<vector<int>> res;
int minDistance=INT_MAX;
for(int i=1;i<arr.size();i++){
int distance=arr[i]-arr[i-1];
if(distance<minDistance){
minDistance=distance;
res.clear();
res.push_back({
arr[i-1],arr[i]});
}
else if(distance==minDistance){
res.push_back({
arr[i-1],arr[i]});
}
}
return res;
}
};
边栏推荐
- Solution to the palindrome string (Luogu p5041 haoi2009)
- Scope of inline symbol
- Daily question 1688 Number of matches in the competition
- Configuration and startup of kubedm series-02-kubelet
- CF1634E Fair Share
- [practical skills] technical management of managers with non-technical background
- Cluster script of data warehouse project
- 注解与反射
- Reflection summary of Haut OJ freshmen on Wednesday
- Control Unit 控制部件
猜你喜欢

剑指 Offer 53 - II. 0~n-1中缺失的数字

Wazuh開源主機安全解决方案的簡介與使用體驗
![[jailhouse article] look mum, no VM exits](/img/fe/87e0851d243f14dff96ef1bc350e50.png)
[jailhouse article] look mum, no VM exits

Sword finger offer 06 Print linked list from beginning to end

Typical use cases for knapsacks, queues, and stacks

Fried chicken nuggets and fifa22

Introduction et expérience de wazuh open source host Security Solution

剑指 Offer 06.从头到尾打印链表

Dynamic planning solution ideas and summary (30000 words)

2017 USP Try-outs C. Coprimes
随机推荐
Fried chicken nuggets and fifa22
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
PC register
The number of enclaves
浅谈JVM(面试常考)
Daily question 2006 Number of pairs whose absolute value of difference is k
Web APIs DOM node
卷积神经网络——卷积层
One question per day 2047 Number of valid words in the sentence
Collection: programming related websites and books
A problem and solution of recording QT memory leakage
How to adjust bugs in general projects ----- take you through the whole process by hand
Scope of inline symbol
High precision subtraction
API related to TCP connection
Brief introduction to tcp/ip protocol stack
Binary search template
剑指 Offer 06.从头到尾打印链表
Daily question 1342 Number of operations to change the number to 0
[jailhouse article] jailhouse hypervisor