当前位置:网站首页>[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
边栏推荐
猜你喜欢
WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
Quelques suggestions pour la conception de l'interface
Win11无法将值写入注册表项如何解决?
Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
Understand the reading, writing and creation of files in go language
See how Tencent does interface automation testing
随机推荐
MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
Go language notes (4) go common management commands
How does wincc7.5 SP1 find variables and their positions through cross indexing?
What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
jekins初始化密码没有或找不到
Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
LeetCode 871. Minimum refueling times
CDGA|数据治理不得不坚持的六个原则
How does win11 search for wireless displays? Win11 method of finding wireless display device
剑指 Offer II 80-100(持续更新)
Jekins initialization password not found or not found
电脑怎么保存网页到桌面上使用
LeetCode+ 81 - 85 单调栈专题
The problem of the maximum difference between the left and right maxima
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
LeetCode 7. 整数反转
Record the online bug solving list (unfinished to be continued 7/4)
Stack: how to realize the judgment of valid brackets?