当前位置:网站首页>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;
}
};
边栏推荐
- Industrial Internet has greater development potential and more industry scenarios
- 5G电视难成竞争优势,视频资源成中国广电最后武器
- LVGL 8.2 LED
- remount of the / superblock failed: Permission denied
- Talk about 10 tips to ensure thread safety
- Graduation season - personal summary
- (1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!
- 利用Shap值进行异常值检测
- C language personal address book management system
- 毕业季-个人总结
猜你喜欢
Why do domestic mobile phone users choose iPhone when changing a mobile phone?
Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)
Compile oglpg-9th-edition source code with clion
The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
5G电视难成竞争优势,视频资源成中国广电最后武器
(1) The standard of performance tuning and the correct posture for tuning - if you have performance problems, go to the heapdump performance community!
What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
LVGL 8.2 Line
LVGL 8.2 Line
Test evaluation of software testing
随机推荐
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
5G电视难成竞争优势,视频资源成中国广电最后武器
LVGL 8.2 Line
Sqlserver functions, creation and use of stored procedures
What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
函数计算异步任务能力介绍 - 任务触发去重
现代控制理论入门+理解
Ultrasonic distance meter based on 51 single chip microcomputer
Progress in architecture
Free, easy-to-use, powerful lightweight note taking software evaluation: drafts, apple memo, flomo, keep, flowus, agenda, sidenote, workflow
ML之shap:基于boston波士顿房价回归预测数据集利用Shap值对LiR线性回归模型实现可解释性案例
LVGL 8.2 Line wrap, recoloring and scrolling
Talk about 10 tips to ensure thread safety
自动控制原理快速入门+理解
LVGL 8.2 Draw label with gradient color
One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
Practical puzzle solving | how to extract irregular ROI regions in opencv
关于FPGA底层资源的细节问题
Ml: introduction, principle, use method and detailed introduction of classic cases of snap value
LVGL 8.2 text shadow