当前位置:网站首页>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;
}
};
边栏推荐
- MySQL triggers
- LVGL 8.2 Menu
- leetcode:6110. The number of incremental paths in the grid graph [DFS + cache]
- An overview of 2D human posture estimation
- Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
- LVGL 8.2 List
- The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
- Ultrasonic distance meter based on 51 single chip microcomputer
- LVGL 8.2 Menu
- Industrial Internet has greater development potential and more industry scenarios
猜你喜欢
开发中常见问题总结
Detailed explanation of visual studio debugging methods
leetcode:6109. Number of people who know the secret [definition of DP]
Wt588f02b-8s (c006_03) single chip voice IC scheme enables smart doorbell design to reduce cost and increase efficiency
[C language] Pointer written test questions
Sqlserver functions, creation and use of stored procedures
电商系统中红包活动设计
leetcode:6110. The number of incremental paths in the grid graph [DFS + cache]
No servers available for service: xxxx
软件测试之测试评估
随机推荐
Transplant tinyplay for imx6q development board QT system
[MySQL from introduction to proficiency] [advanced chapter] (IV) MySQL permission management and control
Deep learning 7 transformer series instance segmentation mask2former
Detailed explanation of visual studio debugging methods
第十六章 字符串本地化和消息字典(二)
C language book rental management system
架构方面的进步
No servers available for service: xxxx
实战解惑 | OpenCV中如何提取不规则ROI区域
LVGL 8.2 Menu
Detailed analysis of pytorch's automatic derivation mechanism, pytorch's core magic
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
如何配和弦
Nowcoder rearrange linked list
codeforce:C. Sum of Substrings【边界处理 + 贡献思维 + 灵光一现】
各大主流编程语言性能PK,结果出乎意料
Opencv learning notes - linear filtering: box filtering, mean filtering, Gaussian filtering
Industrial Internet has greater development potential and more industry scenarios
Digi restarts XBee Pro S2C production. Some differences need to be noted
LVGL 8.2 List