当前位置:网站首页>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;
}
};
边栏推荐
- leetcode:6109. 知道秘密的人数【dp的定义】
- Alcohol driving monitoring system based on stm32+ Huawei cloud IOT design
- Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on GPIO)
- Solutions aux problèmes d'utilisation de l'au ou du povo 2 dans le riz rouge k20pro MIUI 12.5
- Digi XBee 3 rf: 4 protocols, 3 packages, 10 major functions
- 利用Shap值进行异常值检测
- Abnormal value detection using shap value
- Xcode abnormal pictures cause IPA packet size problems
- leetcode:6109. Number of people who know the secret [definition of DP]
- Programmers exposed that they took private jobs: they took more than 30 orders in 10 months, with a net income of 400000
猜你喜欢
《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
Comment configurer un accord
[information retrieval] link analysis
SqlServer函数,存储过程的创建和使用
LVGL 8.2 Line
What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
Practical puzzle solving | how to extract irregular ROI regions in opencv
Count the running time of PHP program and set the maximum running time of PHP
Node mongodb installation
Programmers exposed that they took private jobs: they took more than 30 orders in 10 months, with a net income of 400000
随机推荐
Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
Comment configurer un accord
Ali was laid off employees, looking for a job n day, headhunters came bad news
remount of the / superblock failed: Permission denied
Why do domestic mobile phone users choose iPhone when changing a mobile phone?
[MySQL from introduction to proficiency] [advanced chapter] (IV) MySQL permission management and control
Popular framework: the use of glide
Digi XBee 3 rf: 4 protocols, 3 packages, 10 major functions
SqlServer函数,存储过程的创建和使用
Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态
Leetcode 61: rotating linked list
(1) The standard of performance tuning and the correct posture for tuning - if you have performance problems, go to the heapdump performance community!
Query optimizer for SQL optimization
Nowcoder reverse linked list
C language set operation
LVGL 8.2 Line
No servers available for service: xxxx
毕业季-个人总结
The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
架构方面的进步