当前位置:网站首页>[1200. Différence absolue minimale]
[1200. Différence absolue minimale]
2022-07-04 20:56:00 【Qianbei】
Source::Boucle de force(LeetCode)
Description:
Pour vous donner un tableau entier arr,Chacun de ces éléments C'est différent..
Trouvez toutes les paires d'éléments avec la plus petite différence absolue,Et revenir dans l'ordre croissant.
Exemple 1:
Entrée:arr = [4,2,1,3]
Produits:[[1,2],[2,3],[3,4]]
Exemple 2:
Entrée:arr = [1,3,6,10,15]
Produits:[[1,3]]
Exemple 3:
Entrée:arr = [3,8,-10,23,19,-4,-14,27]
Produits:[[-14,-10],[19,23],[23,27]]
Conseils:
- 2 <= arr.length <= 105
- -106 <= arr[i] <= 106
Méthodes:Trier + Une traversée
Idées et algorithmes

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;
}
};
Temps d'exécution:52 ms, Dans tous les C++ Battu dans la soumission98.35%Utilisateurs de
Consommation de mémoire:31.3 MB, Dans tous les C++ Battu dans la soumission86.32%Utilisateurs de
author:LeetCode-Solution
边栏推荐
- hash 表的概念及应用
- Practical examples of node strong cache and negotiation cache
- Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t
- 针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
- ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
- 字节测试工程师十年经验直击UI 自动化测试痛点
- idea恢复默认快捷键
- jekins初始化密码没有或找不到
- 精选综述 | 用于白内障分级/分类的机器学习技术
- Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法
猜你喜欢

How does the computer save web pages to the desktop for use
![NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]](/img/79/82763392e74d102921b4e8e601d4c6.png)
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]

Automatic generation of interface automatic test cases by actual operation

分析伦敦银走势图的技巧

MySQL - database query - use of aggregate function, aggregate query, grouping query

Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
![[in-depth learning] review pytoch's 19 loss functions](/img/c1/07650a6755d5c64ebf8ce370456fb7.png)
[in-depth learning] review pytoch's 19 loss functions

Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t

Common verification rules of form components -1 (continuously updating ~)

【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
随机推荐
The concept and application of hash table
Go language notes (4) go common management commands
Sword finger offer II 80-100 (continuous update)
Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
Automatic generation of interface automatic test cases by actual operation
uniapp 富文本编辑器使用
MySQL statement execution details
How does the computer save web pages to the desktop for use
Implementation of redis distributed lock
九齐NY8B062D MCU规格书/datasheet
电脑怎么保存网页到桌面上使用
Go notes (1) go language introduction and characteristics
【1200. 最小绝对差】
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
接口设计时的一些建议
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
RFID仓储管理系统解决方案的优点
The problem of the maximum difference between the left and right maxima
Record the online bug solving list (unfinished to be continued 7/4)
LeetCode 7. Integer inversion
