当前位置:网站首页>[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
边栏推荐
- What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
- Implementation of redis distributed lock
- Hash哈希竞猜游戏系统开发如何开发丨哈希竞猜游戏系统开发(多套案例)
- Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法
- 《动手学深度学习》(三) -- 卷积神经网络 CNN
- 九齐NY8B062D MCU规格书/datasheet
- BFC interview Brief
- uniapp 富文本编辑器使用
- Cdga | six principles that data governance has to adhere to
- The problem of the maximum difference between the left and right maxima
猜你喜欢
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
How to adapt your games to different sizes of mobile screen
The concept and application of hash table
So this is the BGP agreement
Ten years' experience of byte test engineer directly hits the pain point of UI automation test
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
What should I do if my computer sharing printer refuses access
【深度学习】一文看尽Pytorch之十九种损失函数
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
随机推荐
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
Implementation of redis distributed lock
Go notes (1) go language introduction and characteristics
九齐NY8B062D MCU规格书/datasheet
Hash quiz game system development how to develop hash quiz game system development (multiple cases)
记录线上bug解决list(未完待续7/4)
go笔记(1)go语言介绍以及特点
jekins初始化密码没有或找不到
After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
[in-depth learning] review pytoch's 19 loss functions
LeetCode 7. 整数反转
Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
Flet tutorial 05 outlinedbutton basic introduction (tutorial includes source code)
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
acwing 3302. 表达式求值
mysql语句执行详解
【申博攻略】六.如何联系心仪的博导
word中插入图片后,图片上方有一空行,且删除后布局变乱
Reinforcement learning - learning notes 2 | value learning
剑指 Offer II 80-100(持续更新)