当前位置:网站首页>【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
边栏推荐
- What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
- Go notes (1) go language introduction and characteristics
- What ppt writing skills does the classic "pyramid principle" teach us?
- 实操自动生成接口自动化测试用例
- Hash哈希竞猜游戏系统开发如何开发丨哈希竞猜游戏系统开发(多套案例)
- 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
- CDGA|数据治理不得不坚持的六个原则
- So this is the BGP agreement
- Practical examples of node strong cache and negotiation cache
- Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
猜你喜欢
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
Managed service network: application architecture evolution in the cloud native Era
idea配置标准注释
Practical examples of node strong cache and negotiation cache
hash 表的概念及应用
《动手学深度学习》(三) -- 卷积神经网络 CNN
node强缓存和协商缓存实战示例
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
Practice examples to understand JS strong cache negotiation cache
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
随机推荐
How to adapt your games to different sizes of mobile screen
AP8022开关电源小家电ACDC芯片离线式开关电源IC
idea配置标准注释
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
See how Tencent does interface automation testing
go语言笔记(4)go常用管理命令
Automatic generation of interface automatic test cases by actual operation
Common verification rules of form components -1 (continuously updating ~)
What ppt writing skills does the classic "pyramid principle" teach us?
Go language notes (4) go common management commands
哈希(Hash)竞猜游戏系统开发功能分析及源码
Ten years' experience of byte test engineer directly hits the pain point of UI automation test
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
What if the brightness of win11 is locked? Solution to win11 brightness locking
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
How does the computer save web pages to the desktop for use
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏