当前位置:网站首页>[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
边栏推荐
猜你喜欢
【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”
How does the computer save web pages to the desktop for use
How to adapt your games to different sizes of mobile screen
工厂从自动化到数字孪生,图扑能干什么?
Jiuqi ny8b062d MCU specification /datasheet
Practice examples to understand JS strong cache negotiation cache
MySQL - database query - use of aggregate function, aggregate query, grouping query
强化学习-学习笔记2 | 价值学习
实操自动生成接口自动化测试用例
字节测试工程师十年经验直击UI 自动化测试痛点
随机推荐
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
《动手学深度学习》(三) -- 卷积神经网络 CNN
Ten years' experience of byte test engineer directly hits the pain point of UI automation test
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
JS closure
mysql语句执行详解
uniapp 富文本编辑器使用
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
Hands on deep learning (III) -- convolutional neural network CNN
What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
Play the music of youth
BFC面试简述
GVM使用
浏览器渲染页面过程
Why is TCP three handshakes and four waves
九齐NY8B062D MCU规格书/datasheet
Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法
LeetCode 7. 整数反转