当前位置:网站首页>【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
边栏推荐
- ACM组合计数入门
- [today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
- 字节测试工程师十年经验直击UI 自动化测试痛点
- Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t
- Flet教程之 05 OutlinedButton基础入门(教程含源码)
- 电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
- FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
- 《动手学深度学习》(三) -- 卷积神经网络 CNN
- 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
猜你喜欢

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

WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?

How does wincc7.5 SP1 find variables and their positions through cross indexing?

Idea restore default shortcut key

What if the computer page cannot be full screen? The solution of win11 page cannot be full screen

So this is the BGP agreement

idea配置标准注释

Flet教程之 05 OutlinedButton基础入门(教程含源码)

Jiuqi ny8b062d MCU specification /datasheet

精选综述 | 用于白内障分级/分类的机器学习技术
随机推荐
实操自动生成接口自动化测试用例
Idea configuration standard notes
精选综述 | 用于白内障分级/分类的机器学习技术
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
Selected review | machine learning technology for Cataract Classification / classification
【解决方案】PaddlePaddle 2.x调用静态图模式
工厂从自动化到数字孪生,图扑能干什么?
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
记一次重复造轮子(Obsidian 插件设置说明汉化)
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
See how Tencent does interface automation testing
So this is the BGP agreement
Alibaba testers use UI automated testing to achieve element positioning
Flet教程之 05 OutlinedButton基础入门(教程含源码)
左右最值最大差问题
Flet教程之 08 AppBar工具栏基础入门(教程含源码)
Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t
九齐NY8B062D MCU规格书/datasheet
The problem of the maximum difference between the left and right maxima
Idea case shortcut
