当前位置:网站首页>[1200. Minimum absolute difference]
[1200. Minimum absolute difference]
2022-07-04 20:57:00 【[email protected]】
source : Power button (LeetCode)
describe :
Here's an array of integers arr, Each of these elements is inequality .
Please find all the elements with the least absolute difference , And return in ascending order .
Example 1:
Input :arr = [4,2,1,3]
Output :[[1,2],[2,3],[3,4]]
Example 2:
Input :arr = [1,3,6,10,15]
Output :[[1,3]]
Example 3:
Input :arr = [3,8,-10,23,19,-4,-14,27]
Output :[[-14,-10],[19,23],[23,27]]
Tips :
- 2 <= arr.length <= 105
- -106 <= arr[i] <= 106
Method : Sort + One traverse
Ideas and algorithms

Code :
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;
}
};
Execution time :52 ms, In all C++ Defeated in submission 98.35% Users of
Memory consumption :31.3 MB, In all C++ Defeated in submission 86.32% Users of
author:LeetCode-Solution
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041936120104.html
边栏推荐
- 看腾讯大老如何做接口自动化测试
- 【申博攻略】六.如何联系心仪的博导
- Alibaba testers use UI automated testing to achieve element positioning
- See how Tencent does interface automation testing
- After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
- idea配置标准注释
- Win11无法将值写入注册表项如何解决?
- 电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
- RFID仓库管理系统解决方案有哪些功能模块
- 【深度学习】一文看尽Pytorch之十九种损失函数
猜你喜欢

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

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

Hands on deep learning (III) -- convolutional neural network CNN

MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询

ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise

测试员的算法面试题-找众数

Jiuqi ny8b062d MCU specification /datasheet

针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS

Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS

How does the computer save web pages to the desktop for use
随机推荐
What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
LeetCode 7. Integer inversion
MySQL - database query - use of aggregate function, aggregate query, grouping query
哈希(Hash)竞猜游戏系统开发功能分析及源码
What if win11u disk refuses access? An effective solution to win11u disk access denial
Idea plug-in
哈希表、哈希函数、布隆过滤器、一致性哈希
Après l'insertion de l'image dans le mot, il y a une ligne vide au - dessus de l'image, et la disposition est désordonnée après la suppression
扩展你的KUBECTL功能
Flet教程之 07 PopupMenuButton基础入门(教程含源码)
LeetCode 7. 整数反转
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
【解决方案】PaddlePaddle 2.x调用静态图模式
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
MySQL statement execution details
LeetCode+ 81 - 85 单调栈专题
So this is the BGP agreement
强化学习-学习笔记2 | 价值学习
6月“墨力原创作者计划”获奖名单公布!邀您共话国产数据库
Browser render page pass
