当前位置:网站首页>每日一题-LeetCode1200-最小绝对差-数组-排序
每日一题-LeetCode1200-最小绝对差-数组-排序
2022-07-04 20:23:00 【李烦烦搞快点】
Note:
因为没有重复的元素,所以先给数组排序,然后每两个一组判断他俩差的绝对值,如果出现更小的了,就把答案数组清空,放入当前的组合,然后更新一下最小值
或者等于最小值,那就直接扔进答案数组里,否则就两个指针往后移动
代码如下:
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;
}
};
边栏推荐
猜你喜欢

UTF encoding and character set in golang

Huawei ENSP simulator configures ACL access control list

Render function and virtual DOM

分析伦敦银走势图的技巧

测试员的算法面试题-找众数

6月“墨力原创作者计划”获奖名单公布!邀您共话国产数据库

In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills

杰理之AD 系列 MIDI 功能说明【篇】

c语言函数形参自增自减情况分析

What are the functional modules of RFID warehouse management system solution
随机推荐
吐槽 B 站收费,是怪它没钱么?
torch.tensor和torch.Tensor的区别
实战模拟│JWT 登录认证
LeetCode+ 81 - 85 单调栈专题
【申博攻略】六.如何联系心仪的博导
Huawei simulator ENSP common commands
华为ensp模拟器 三层交换机
TweenMax表情按钮js特效
【optimtool.unconstrain】无约束优化工具箱
Some suggestions for interface design
网络命名空间
In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
【Try to Hack】宽字节注入
6月“墨力原创作者计划”获奖名单公布!邀您共话国产数据库
搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
FastDfs的快速入门,三分钟带你上传下载文件到云服务器
Nmap scan
Day24:文件系统
nmap扫描
