当前位置:网站首页>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;
}
};
边栏推荐
- scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
- Chapter 16 string localization and message Dictionary (2)
- Ml: introduction, principle, use method and detailed introduction of classic cases of snap value
- Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
- Transplant tinyplay for imx6q development board QT system
- Solutions to the problems of miui12.5 red rice k20pro using Au or povo2
- LVGL 8.2 Sorting a List using up and down buttons
- 聊聊保证线程安全的 10 个小技巧
- Abnormal value detection using shap value
- 關於miui12.5 紅米k20pro用au或者povo2出現問題的解决辦法
猜你喜欢
The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
自动控制原理快速入门+理解
Ranking list of databases in July: mongodb and Oracle scores fell the most
LVGL 8.2 Line wrap, recoloring and scrolling
Xcode abnormal pictures cause IPA packet size problems
Ultrasonic distance meter based on 51 single chip microcomputer
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
Free, easy-to-use, powerful lightweight note taking software evaluation: drafts, apple memo, flomo, keep, flowus, agenda, sidenote, workflow
各大主流编程语言性能PK,结果出乎意料
随机推荐
Test evaluation of software testing
Nowcoder reverse linked list
开发中常见问题总结
数据湖(十三):Spark与Iceberg整合DDL操作
MySQL的触发器
Programmer turns direction
C language book rental management system
5G电视难成竞争优势,视频资源成中国广电最后武器
Node mongodb installation
LVGL 8.2 List
尊重他人的行为
C language course design questions
Ranking list of databases in July: mongodb and Oracle scores fell the most
Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on GPIO)
C language set operation
WT588F02B-8S(C006_03)单芯片语音ic方案为智能门铃设计降本增效赋能
Deep learning 7 transformer series instance segmentation mask2former
Opencv3.2 and opencv2.4 installation
The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
Gin integrated Alipay payment