当前位置:网站首页>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;
}
};
边栏推荐
- Transform optimization problems into decision-making problems
- Drawing dynamic 3D circle with pure C language
- Bit mask of bit operation
- lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- 游戏商城毕业设计
- After setting up the database and website When you open the app for testing, it shows that the server is being maintained
- 【Jailhouse 文章】Jailhouse Hypervisor
- kubeadm系列-01-preflight究竟有多少check
- Full Permutation Code (recursive writing)
- Time of process
猜你喜欢
Typical use cases for knapsacks, queues, and stacks
Personal developed penetration testing tool Satania v1.2 update
【云原生】微服务之Feign自定义配置的记录
[cloud native] record of feign custom configuration of microservices
[jailhouse article] jailhouse hypervisor
API related to TCP connection
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
Sword finger offer 35 Replication of complex linked list
SAP-修改系统表数据的方法
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
随机推荐
卷积神经网络——卷积层
kubeadm系列-02-kubelet的配置和启动
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
数仓项目的集群脚本
Alu logic operation unit
Kubedm series-00-overview
Implement a fixed capacity stack
Cluster script of data warehouse project
Scope of inline symbol
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
挂起等待锁 vs 自旋锁(两者的使用场合)
Daily question 1688 Number of matches in the competition
Dichotomy, discretization, etc
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
过拟合与正则化
Light a light with stm32
Convolution neural network -- convolution layer
Acwing 4301. Truncated sequence
API related to TCP connection
智慧工地“水电能耗在线监测系统”