当前位置:网站首页>[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
边栏推荐
- 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
- 科普达人丨一文看懂阿里云的秘密武器“神龙架构”
- LeetCode 871. 最低加油次数
- Go language notes (4) go common management commands
- Quelques suggestions pour la conception de l'interface
- Common verification rules of form components -1 (continuously updating ~)
- Idea plug-in
- 面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
- Win11怎么搜索无线显示器?Win11查找无线显示器设备的方法
- So this is the BGP agreement
猜你喜欢

Form组件常用校验规则-1(持续更新中~)

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

【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”

RFID仓储管理系统解决方案的优点
Understand the reading, writing and creation of files in go language

接口設計時的一些建議

【深度学习】一文看尽Pytorch之十九种损失函数
![[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born](/img/0b/73f0d98a6db813e54074abe199ed98.png)
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
Practice examples to understand JS strong cache negotiation cache

So this is the BGP agreement
随机推荐
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
Flet教程之 06 TextButton基础入门(教程含源码)
Function analysis and source code of hash guessing game system development
Jekins initialization password not found or not found
How does the computer save web pages to the desktop for use
语义化标签的优势和块级行内元素
Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法
Play the music of youth
What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
栈:如何实现有效括号的判断?
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
Flet tutorial 07 basic introduction to popupmenubutton (tutorial includes source code)
【解决方案】PaddlePaddle 2.x调用静态图模式
卷积神经网络在深度学习中新发展的5篇论文推荐
Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
扩展你的KUBECTL功能
Flet教程之 07 PopupMenuButton基础入门(教程含源码)
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
最长的可整合子数组的长度
Alibaba testers use UI automated testing to achieve element positioning
