当前位置:网站首页>【1200. 最小絕對差】
【1200. 最小絕對差】
2022-07-04 20:56: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
边栏推荐
- GVM使用
- LeetCode 8. String conversion integer (ATOI)
- 扩展你的KUBECTL功能
- word中使用自动插入题注功能
- FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
- 太方便了,钉钉上就可完成代码发布审批啦!
- After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
- What is the development of block hash quiz game system? Hash quiz game system development (case mature)
- Go language notes (4) go common management commands
- Redis分布式锁的实现
猜你喜欢

九齐NY8B062D MCU规格书/datasheet

阿里测试师用UI自动化测试实现元素定位

面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧

What if the brightness of win11 is locked? Solution to win11 brightness locking

【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt

ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声

Idea restore default shortcut key

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

What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened

From automation to digital twins, what can Tupo do?
随机推荐
How does win11 search for wireless displays? Win11 method of finding wireless display device
企业数字化转型最佳实践案例:基于云的数字化平台系统安全措施简介与参考
go笔记(1)go语言介绍以及特点
看腾讯大老如何做接口自动化测试
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
Jiuqi ny8b062d MCU specification /datasheet
RFID仓储管理系统解决方案的优点
ACM组合计数入门
Understand the reading, writing and creation of files in go language
九齐单片机NY8B062D单按键控制4种LED状态
Reinforcement learning - learning notes 2 | value learning
Win11无法将值写入注册表项如何解决?
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
go笔记(3)Go语言fmt包的用法
word中插入圖片後,圖片上方有一空行,且删除後布局變亂
See how Tencent does interface automation testing
【1200. 最小绝对差】
电脑共享打印机拒绝访问要怎么办
Go notes (1) go language introduction and characteristics
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
