当前位置:网站首页>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;
}
};
边栏推荐
- R语言【数据集的导入导出】
- Wazuh開源主機安全解决方案的簡介與使用體驗
- CF1634E Fair Share
- Simple knapsack, queue and stack with deque
- Collection: programming related websites and books
- 全排列的代码 (递归写法)
- [practical skills] technical management of managers with non-technical background
- The sum of the unique elements of the daily question
- How many checks does kubedm series-01-preflight have
- Daily question 1342 Number of operations to change the number to 0
猜你喜欢
Sword finger offer 04 Search in two-dimensional array
API related to TCP connection
[practical skills] technical management of managers with non-technical background
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
剑指 Offer 06.从头到尾打印链表
Palindrome (csp-s-2021-palin) solution
[jailhouse article] look mum, no VM exits
Gbase database helps the development of digital finance in the Bay Area
Sword finger offer 35 Replication of complex linked list
Sword finger offer 05 Replace spaces
随机推荐
二十六、文件系统API(设备在应用间的共享;目录和文件API)
Personal developed penetration testing tool Satania v1.2 update
Over fitting and regularization
YOLOv5-Shufflenetv2
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
PC寄存器
【Jailhouse 文章】Look Mum, no VM Exits
卷积神经网络简介
A misunderstanding about the console window
CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
卷积神经网络——卷积层
AtCoder Grand Contest 013 E - Placing Squares
剑指 Offer 58 - II. 左旋转字符串
kubeadm系列-00-overview
用STM32点个灯
Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
过拟合与正则化
R language [import and export of dataset]
Introduction et expérience de wazuh open source host Security Solution
In this indifferent world, light crying