当前位置:网站首页>【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
边栏推荐
- 长城证券开户安全吗 股票开户流程网上开户
- Idea plug-in
- 看腾讯大老如何做接口自动化测试
- 电脑怎么保存网页到桌面上使用
- Hash哈希竞猜游戏系统开发如何开发丨哈希竞猜游戏系统开发(多套案例)
- Understand the reading, writing and creation of files in go language
- 左右最值最大差问题
- 二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
- Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
- Sword finger offer II 80-100 (continuous update)
猜你喜欢

托管式服务网络:云原生时代的应用体系架构进化

工厂从自动化到数字孪生,图扑能干什么?

idea配置标准注释

Flet教程之 06 TextButton基础入门(教程含源码)

Ten years' experience of byte test engineer directly hits the pain point of UI automation test

黄金k线图中的三角形有几种?

Sword finger offer II 80-100 (continuous update)

接口设计时的一些建议
Practice examples to understand JS strong cache negotiation cache

Win11无法将值写入注册表项如何解决?
随机推荐
BFC面试简述
黄金k线图中的三角形有几种?
剑指 Offer II 80-100(持续更新)
NetCore3.1 Json web token 中间件
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
强化学习-学习笔记2 | 价值学习
记录线上bug解决list(未完待续7/4)
Related concepts of federal learning and motivation (1)
E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
Flet教程之 06 TextButton基础入门(教程含源码)
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
[in-depth learning] review pytoch's 19 loss functions
【申博攻略】六.如何联系心仪的博导
In operation (i.e. included in) usage of SSRs filter
word中使用自动插入题注功能
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
BFC interview Brief
最长的可整合子数组的长度
