当前位置:网站首页>[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
边栏推荐
- Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
- 记一次重复造轮子(Obsidian 插件设置说明汉化)
- Go language notes (2) some simple applications of go
- Idea configuration standard notes
- Flet教程之 05 OutlinedButton基础入门(教程含源码)
- 九齐单片机NY8B062D单按键控制4种LED状态
- Summary of the mistakes in the use of qpainter in QT gobang man-machine game
- Go notes (1) go language introduction and characteristics
- Redis分布式锁的实现
- 托管式服务网络:云原生时代的应用体系架构进化
猜你喜欢
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
看腾讯大老如何做接口自动化测试
WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
【深度学习】一文看尽Pytorch之十九种损失函数
《动手学深度学习》(三) -- 卷积神经网络 CNN
Sword finger offer II 80-100 (continuous update)
测试员的算法面试题-找众数
From automation to digital twins, what can Tupo do?
电脑怎么保存网页到桌面上使用
uniapp 富文本编辑器使用
随机推荐
黄金k线图中的三角形有几种?
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
Quelques suggestions pour la conception de l'interface
太方便了,钉钉上就可完成代码发布审批啦!
[in-depth learning] review pytoch's 19 loss functions
企业数字化转型最佳实践案例:基于云的数字化平台系统安全措施简介与参考
电脑怎么保存网页到桌面上使用
How does wincc7.5 SP1 find variables and their positions through cross indexing?
tcp为啥是三次握手和四次挥手
Pytorch---使用Pytorch实现LinkNet进行语义分割
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
Go language notes (4) go common management commands
Qt五子棋人机对战画棋子之QPainter的使用误区总结
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
Common verification rules of form components -1 (continuously updating ~)
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
PermissionError: [Errno 13] Permission denied: ‘data.csv‘
After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
Hands on deep learning (III) -- convolutional neural network CNN