当前位置:网站首页>Daily question -leetcode1200- minimum absolute difference - array - sort
Daily question -leetcode1200- minimum absolute difference - array - sort
2022-07-04 21:24:00 【Li Fan, hurry up】
Note:
Because there are no duplicate elements , So sort the array first , Then judge the absolute value of their difference in groups of two , If something smaller appears , Just empty the answer array , Put into the current combination , Then update the minimum value
Or equal to the minimum , Then throw it directly into the answer array , Otherwise, the two pointers will move back
The code is as follows :
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
vector<vector<int>> ans;
int minNum = INT_MAX;
sort(arr.begin(), arr.end());
for(int i = 0, j = 1; j < arr.size(); i ++, j ++){
if(abs(arr[i] - arr[j]) < minNum){
ans.clear();
ans.push_back({
min(arr[i], arr[j]), max(arr[i], arr[j])});
minNum = abs(arr[i] - arr[j]);
}else if(abs(arr[i] - arr[j]) == minNum)
ans.push_back({
min(arr[i], arr[j]), max(arr[i], arr[j])});
}
return ans;
}
};
边栏推荐
- WGCNA analysis basic tutorial summary
- 【微信小程序】协同工作与发布
- [observation] Lenovo: 3x (1+n) smart office solution, releasing the "multiplier effect" of office productivity
- Liu Jincheng won the 2022 China e-commerce industry innovation Figure Award
- Advantages of RFID warehouse management system solution
- Day24:文件系统
- 偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
- Redis pipeline
- 插入排序,选择排序,冒泡排序
- Jerry's ad series MIDI function description [chapter]
猜你喜欢
随机推荐
Introduction to pressure measurement of JMeter
WGCNA分析基本教程总结
maya灯建模
网络命名空间
Huawei ENSP simulator configures DHCP for router
Embedded TC test case
IIC (STM32)
[1200. Différence absolue minimale]
vim异步问题
Difference between ApplicationContext and beanfactory (MS)
杰理之AD 系列 MIDI 功能说明【篇】
Why does invariant mode improve performance
Actual combat simulation │ JWT login authentication
[micro service SCG] use of predict
[solution] paddlepaddle 2 X call static graph mode
杰理之AD 系列 MIDI 功能说明【篇】
刘锦程荣获2022年度中国电商行业创新人物奖
Jerry's ad series MIDI function description [chapter]
华为ensp模拟器实现通信安全(交换机)
uniapp 富文本编辑器使用