当前位置:网站首页>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;
}
};
边栏推荐
- [buuctf.reverse] 151_[FlareOn6]DnsChess
- 多模輸入事件分發機制詳解
- 杰理之AD 系列 MIDI 功能说明【篇】
- Huawei ENSP simulator enables devices of multiple routers to access each other
- SolidWorks工程图添加材料明细表的操作
- [buuctf.reverse] 151_ [FlareOn6]DnsChess
- Roast B station charges, is it because it has no money?
- Routing configuration and connectivity test of Huawei simulator ENSP
- 搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
- 华为ensp模拟器 实现多个路由器的设备可以相互访问
猜你喜欢
随机推荐
奋斗正当时,城链科技战略峰会广州站圆满召开
WGCNA analysis basic tutorial summary
HMS Core 机器学习服务
Golang中UTF编码和字符集
[solution] paddlepaddle 2 X call static graph mode
网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法
Routing configuration and connectivity test of Huawei simulator ENSP
D3.js+Three.js数据可视化3d地球js特效
刘锦程荣获2022年度中国电商行业创新人物奖
实战模拟│JWT 登录认证
A quick start to fastdfs takes you three minutes to upload and download files to the ECS
nmap扫描
冰河的海报封面
Detailed explanation of multi-mode input event distribution mechanism
WGCNA分析基本教程总结
minidom 模塊寫入和解析 XML
福昕PDF编辑器v10.1.8绿色版
redis布隆过滤器
c语言函数形参自增自减情况分析
Use of redis publish subscription