当前位置:网站首页>LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路
LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路
2022-07-04 13:24:00 【HERODING23】

解题思路:
首先排序,由于最小的绝对值差一定是相邻数字之间,所以两两遍历进行比较,设定一个diff差值作为最小差值,遇到更小的就更新,并清空ans,代码如下:
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
int n = arr.size();
sort(arr.begin(), arr.end());
int diff = arr[1] - arr[0];
vector<vector<int>> ans;
for(int i = 1; i < n; i ++) {
if(arr[i] - arr[i - 1] == diff) {
ans.push_back({
arr[i - 1], arr[i]});
} else if(arr[i] - arr[i - 1] < diff) {
diff = arr[i] - arr[i - 1];
ans.clear();
ans.push_back({
arr[i - 1], arr[i]});
} else continue;
}
return ans;
}
};
边栏推荐
- Data Lake (13): spark and iceberg integrate DDL operations
- Docker compose public network deployment redis sentinel mode
- No servers available for service: xxxx
- Ranking list of databases in July: mongodb and Oracle scores fell the most
- Data center concept
- Some problems and ideas of data embedding point
- 各大主流编程语言性能PK,结果出乎意料
- 【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
- Gin integrated Alipay payment
- Wt588f02b-8s (c006_03) single chip voice IC scheme enables smart doorbell design to reduce cost and increase efficiency
猜你喜欢
随机推荐
STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于GPIO)
Deep learning 7 transformer series instance segmentation mask2former
(1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
Some problems and ideas of data embedding point
[C language] Pointer written test questions
LVGL 8.2 List
LVLG 8.2 circular scrolling animation of a label
【云原生】我怎么会和这个数据库杠上了?
Data Lake (13): spark and iceberg integrate DDL operations
One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
一种架构来完成所有任务—Transformer架构正在以一己之力统一AI江湖
How to match chords
No servers available for service: xxxx
ML:SHAP值的简介、原理、使用方法、经典案例之详细攻略
Test evaluation of software testing
scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
Solutions aux problèmes d'utilisation de l'au ou du povo 2 dans le riz rouge k20pro MIUI 12.5
Wt588f02b-8s (c006_03) single chip voice IC scheme enables smart doorbell design to reduce cost and increase efficiency
LVGL 8.2 Sorting a List using up and down buttons









