当前位置:网站首页>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;
}
};
边栏推荐
- Some problems and ideas of data embedding point
- es6模块化
- LVGL 8.2 Sorting a List using up and down buttons
- Ml: introduction, principle, use method and detailed introduction of classic cases of snap value
- leetcode:6109. Number of people who know the secret [definition of DP]
- 一文概览2D人体姿态估计
- C language set operation
- Detailed explanation of visual studio debugging methods
- 5g TV cannot become a competitive advantage, and video resources become the last weapon of China's Radio and television
- Leetcode 61: rotating linked list
猜你喜欢

Digi XBee 3 RF: 4个协议,3种封装,10个大功能
![[information retrieval] link analysis](/img/dc/4956e8e21d8ce6be1db1822d19ca61.png)
[information retrieval] link analysis
![leetcode:6110. The number of incremental paths in the grid graph [DFS + cache]](/img/48/56ed03b21709a97fd55c8cccf98092.png)
leetcode:6110. The number of incremental paths in the grid graph [DFS + cache]

Explain of SQL optimization

Query optimizer for SQL optimization

Nowcoder rearrange linked list

Ultrasonic distance meter based on 51 single chip microcomputer

Leetcode 61: rotating linked list

leecode学习笔记-约瑟夫问题

Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)
随机推荐
Transplant tinyplay for imx6q development board QT system
[algorithm leetcode] interview question 04.03 Specific depth node linked list (Multilingual Implementation)
A keepalived high availability accident made me learn it again
开发中常见问题总结
Programmer turns direction
MySQL stored procedure exercise
C language programming
flink sql-client. SH tutorial
MySQL triggers
Ranking list of databases in July: mongodb and Oracle scores fell the most
Practical puzzle solving | how to extract irregular ROI regions in opencv
Why do domestic mobile phone users choose iPhone when changing a mobile phone?
实战解惑 | OpenCV中如何提取不规则ROI区域
程序员自曝接私活:10个月时间接了30多个单子,纯收入40万
[cloud native] how can I compete with this database?
各大主流编程语言性能PK,结果出乎意料
Free, easy-to-use, powerful lightweight note taking software evaluation: drafts, apple memo, flomo, keep, flowus, agenda, sidenote, workflow
leetcode:6109. 知道秘密的人数【dp的定义】
数据湖(十三):Spark与Iceberg整合DDL操作
Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on GPIO)