当前位置:网站首页>Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
2022-07-04 14:46:00 【HERODING23】

Their thinking :
Sort first , Because the smallest absolute value difference must be between adjacent numbers , So compare two iterations , Set up a diff The difference is taken as the minimum difference , Update when encountering smaller ones , And empty ans, The code is as follows :
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;
}
};
边栏推荐
- Codeforce:c. sum of substrings
- Digi XBee 3 rf: 4 protocols, 3 packages, 10 major functions
- 【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制
- es6模块化
- 曝光一下阿里的工资待遇和职位级别
- (1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!
- leetcode:6110. 网格图中递增路径的数目【dfs + cache】
- First experience of ViewModel
- C language small commodity management system
- LVGL 8.2 text shadow
猜你喜欢

现代控制理论入门+理解

Ali was laid off employees, looking for a job n day, headhunters came bad news

自动控制原理快速入门+理解

Combined with case: the usage of the lowest API (processfunction) in Flink framework

Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on GPIO)

Summary of common problems in development

曝光一下阿里的工资待遇和职位级别

Alcohol driving monitoring system based on stm32+ Huawei cloud IOT design

No servers available for service: xxxx
![LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路](/img/4a/6763e3fbdeaf9de673fbe8eaf96858.png)
LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路
随机推荐
【算法leetcode】面试题 04.03. 特定深度节点链表(多语言实现)
Comment configurer un accord
LVGL 8.2 Menu
LVLG 8.2 circular scrolling animation of a label
产业互联网则具备更大的发展潜能,具备更多的行业场景
LVGL 8.2 keyboard
LVGL 8.2 text shadow
【C语言】指针笔试题
es6模块化
如何配和弦
炒股网上开户安全吗?会不会被骗。
Ali was laid off employees, looking for a job n day, headhunters came bad news
LVGL 8.2 Sorting a List using up and down buttons
LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路
First experience of ViewModel
尊重他人的行为
LVGL 8.2 text shadow
Wt588f02b-8s (c006_03) single chip voice IC scheme enables smart doorbell design to reduce cost and increase efficiency
Is it safe to open an account online for stock speculation? Will you be cheated.
Docker compose public network deployment redis sentinel mode