当前位置:网站首页>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;
}
};
边栏推荐
- CF1634E Fair Share
- 中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
- 6. Logistic model
- One question per day 2047 Number of valid words in the sentence
- kubeadm系列-00-overview
- R语言【数据集的导入导出】
- Hang wait lock vs spin lock (where both are used)
- Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
- 每日一题-搜索二维矩阵ps二维数组的查找
- Palindrome (csp-s-2021-palin) solution
猜你喜欢

Reader writer model

Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module

Acwing 4300. Two operations

Sword finger offer 04 Search in two-dimensional array

Wazuh开源主机安全解决方案的简介与使用体验

The connection and solution between the shortest Hamilton path and the traveling salesman problem

In this indifferent world, light crying

从Dijkstra的图灵奖演讲论科技创业者特点

Sword finger offer 53 - ii Missing numbers from 0 to n-1

Codeforces round 712 (Div. 2) d. 3-coloring (construction)
随机推荐
The number of enclaves
Hang wait lock vs spin lock (where both are used)
Personal developed penetration testing tool Satania v1.2 update
Pointnet++ learning
Little known skills of Task Manager
Bit mask of bit operation
Sword finger offer 35 Replication of complex linked list
Light a light with stm32
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
Binary search template
Typical use cases for knapsacks, queues, and stacks
Chapter 6 data flow modeling - after class exercises
One question per day 2047 Number of valid words in the sentence
Common optimization methods
Gbase database helps the development of digital finance in the Bay Area
Scope of inline symbol
剑指 Offer 04. 二维数组中的查找
Transform optimization problems into decision-making problems
AtCoder Grand Contest 013 E - Placing Squares
After setting up the database and website When you open the app for testing, it shows that the server is being maintained