当前位置:网站首页>LeetCode 1200 最小絕對差[排序] HERODING的LeetCode之路
LeetCode 1200 最小絕對差[排序] HERODING的LeetCode之路
2022-07-04 14:45: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;
}
};
边栏推荐
- Talk about 10 tips to ensure thread safety
- Codeforce:c. sum of substrings
- Why do domestic mobile phone users choose iPhone when changing a mobile phone?
- Ali was laid off employees, looking for a job n day, headhunters came bad news
- Summary of common problems in development
- ML:SHAP值的简介、原理、使用方法、经典案例之详细攻略
- [C language] Pointer written test questions
- (1) The standard of performance tuning and the correct posture for tuning - if you have performance problems, go to the heapdump performance community!
- AI与生命科学
- How to match chords
猜你喜欢

Real time data warehouse

Query optimizer for SQL optimization

(1) The standard of performance tuning and the correct posture for tuning - if you have performance problems, go to the heapdump performance community!

Why do domestic mobile phone users choose iPhone when changing a mobile phone?

Red envelope activity design in e-commerce system

Opencv learning notes - linear filtering: box filtering, mean filtering, Gaussian filtering

LVGL 8.2 Line

实战解惑 | OpenCV中如何提取不规则ROI区域

Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
![leetcode:6109. Number of people who know the secret [definition of DP]](/img/95/03e2606b249f26db052cf5075041c1.png)
leetcode:6109. Number of people who know the secret [definition of DP]
随机推荐
LVGL 8.2 Line
MySQL triggers
Data center concept
The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
LVGL 8.2 List
电商系统中红包活动设计
Compile oglpg-9th-edition source code with clion
Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
ML之shap:基于boston波士顿房价回归预测数据集利用Shap值对LiR线性回归模型实现可解释性案例
函数计算异步任务能力介绍 - 任务触发去重
尊重他人的行为
scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
PyTorch的自动求导机制详细解析,PyTorch的核心魔法
【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制
An overview of 2D human posture estimation
阿里被裁员工,找工作第N天,猎头又传来噩耗...
Ultrasonic distance meter based on 51 single chip microcomputer
《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
A collection of classic papers on convolutional neural networks (deep learning classification)
Yyds dry goods inventory # solve the real problem of famous enterprises: continuous maximum sum