当前位置:网站首页>[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
边栏推荐
- BFC interview Brief
- How to adapt your games to different sizes of mobile screen
- Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法
- 强化学习-学习笔记2 | 价值学习
- What if the brightness of win11 is locked? Solution to win11 brightness locking
- FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
- Flet教程之 08 AppBar工具栏基础入门(教程含源码)
- uniapp 富文本编辑器使用
- Idea case shortcut
- idea配置标准注释
猜你喜欢
Selected review | machine learning technology for Cataract Classification / classification
uniapp 富文本编辑器使用
idea配置标准注释
哈希表、哈希函数、布隆过滤器、一致性哈希
What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
What if win11u disk refuses access? An effective solution to win11u disk access denial
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
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
Practical examples of node strong cache and negotiation cache
What if the brightness of win11 is locked? Solution to win11 brightness locking
随机推荐
go笔记(3)Go语言fmt包的用法
RFID仓库管理系统解决方案有哪些功能模块
In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
Play the music of youth
LeetCode 7. 整数反转
Reinforcement learning - learning notes 2 | value learning
E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
精选综述 | 用于白内障分级/分类的机器学习技术
奏响青春的乐章
扩展你的KUBECTL功能
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
Common verification rules of form components -1 (continuously updating ~)
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
uniapp 富文本编辑器使用
6月“墨力原创作者计划”获奖名单公布!邀您共话国产数据库
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
go笔记(1)go语言介绍以及特点
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
什么是区块哈希竞猜游戏系统开发?哈希竞猜游戏系统开发(案例成熟)