当前位置:网站首页>【1200. 最小绝对差】
【1200. 最小绝对差】
2022-07-04 19:36:00 【千北@】
来源:力扣(LeetCode)
描述:
给你个整数数组 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 <= 105
- -106 <= arr[i] <= 106
方法:排序 + 一次遍历
思路与算法

代码:
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
int n = arr.size();
sort(arr.begin(), arr.end());
int best = INT_MAX;
vector<vector<int>> ans;
for (int i = 0; i < n - 1; ++i) {
if (int delta = arr[i + 1] - arr[i]; delta < best) {
best = delta;
ans = {
{
arr[i], arr[i + 1]}};
}
else if (delta == best) {
ans.emplace_back(initializer_list<int>{
arr[i], arr[i + 1]});
}
}
return ans;
}
};
执行用时:52 ms, 在所有 C++ 提交中击败了98.35%的用户
内存消耗:31.3 MB, 在所有 C++ 提交中击败了86.32%的用户
author:LeetCode-Solution
边栏推荐
- 黄金k线图中的三角形有几种?
- ACM组合计数入门
- Length of the longest integrable subarray
- RFID仓储管理系统解决方案的优点
- Go notes (1) go language introduction and characteristics
- WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
- Jekins initialization password not found or not found
- How does win11 search for wireless displays? Win11 method of finding wireless display device
- 阿里测试师用UI自动化测试实现元素定位
- Selected review | machine learning technology for Cataract Classification / classification
猜你喜欢

RFID仓储管理系统解决方案的优点

Flet教程之 08 AppBar工具栏基础入门(教程含源码)

Related concepts of federal learning and motivation (1)

Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"

Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)

AP8022开关电源小家电ACDC芯片离线式开关电源IC

LeetCode+ 81 - 85 单调栈专题

In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills

科普达人丨一文看懂阿里云的秘密武器“神龙架构”

Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
随机推荐
Why is the maximum speed the speed of light
记一次重复造轮子(Obsidian 插件设置说明汉化)
What if the brightness of win11 is locked? Solution to win11 brightness locking
测试员的算法面试题-找众数
In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
What should I do if my computer sharing printer refuses access
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
黄金k线图中的三角形有几种?
CDGA|数据治理不得不坚持的六个原则
LeetCode 871. 最低加油次数
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
What if win11u disk refuses access? An effective solution to win11u disk access denial
电脑共享打印机拒绝访问要怎么办
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
Idea plug-in
idea插件
Flet tutorial 07 basic introduction to popupmenubutton (tutorial includes source code)
