当前位置:网站首页>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;
}
};
边栏推荐
- Yyds dry goods inventory # solve the real problem of famous enterprises: continuous maximum sum
- LVGL 8.2 Sorting a List using up and down buttons
- 现代控制理论入门+理解
- Industrial Internet has greater development potential and more industry scenarios
- Leetcode t49: grouping of alphabetic words
- Abnormal value detection using shap value
- LVGL 8.2 List
- leetcode:6110. The number of incremental paths in the grid graph [DFS + cache]
- A collection of classic papers on convolutional neural networks (deep learning classification)
- Why do domestic mobile phone users choose iPhone when changing a mobile phone?
猜你喜欢

10. (map data) offline terrain data processing (for cesium)

各大主流编程语言性能PK,结果出乎意料

Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?

软件测试之测试评估

【C语言】指针笔试题

韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯

LVGL 8.2 Draw label with gradient color

Ultrasonic distance meter based on 51 single chip microcomputer

Talk about 10 tips to ensure thread safety
![[information retrieval] experiment of classification and clustering](/img/05/ee3b3bc4ab79d52b63cdc34305aa57.png)
[information retrieval] experiment of classification and clustering
随机推荐
Ranking list of databases in July: mongodb and Oracle scores fell the most
Abnormal value detection using shap value
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
LVLG 8.2 circular scrolling animation of a label
es6模块化
《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
LVGL 8.2 LED
Data center concept
各大主流编程语言性能PK,结果出乎意料
Data Lake (13): spark and iceberg integrate DDL operations
Chapter 16 string localization and message Dictionary (2)
关于miui12.5 红米k20pro用au或者povo2出现问题的解决办法
炒股网上开户安全吗?会不会被骗。
Nowcoder reverse linked list
leecode学习笔记-约瑟夫问题
LVGL 8.2 Sorting a List using up and down buttons
Graduation season - personal summary
Query optimizer for SQL optimization
Classify boost libraries by function
leetcode:6109. 知道秘密的人数【dp的定义】