当前位置:网站首页>每日一题-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;
}
};
边栏推荐
猜你喜欢

【服务器数据恢复】某品牌服务器存储raid5数据恢复案例

NetWare r7000 Merlin system virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements. Solution, is it necessary to create virtual memory??

【optimtool.unconstrain】无约束优化工具箱

shp数据制作3DTiles白膜

PS竖排英文和数字文字怎么改变方向(变竖直显示)

What are the functional modules of RFID warehouse management system solution

偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”

Automatic generation of interface automatic test cases by actual operation
![[1200. Minimum absolute difference]](/img/fa/4ffbedd8f24c75a20d3eaeaf0430ae.png)
[1200. Minimum absolute difference]

PS vertical English and digital text how to change direction (vertical display)
随机推荐
js 3D爆炸碎片图片切换js特效
Procurement in software development
MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
PermissionError: [Errno 13] Permission denied: ‘data.csv‘
伦敦银走势图分析的新方法
Idea configuration standard notes
Why is TCP three handshakes and four waves
LeetCode 7. Integer inversion
[1200. Minimum absolute difference]
HMS Core 统一扫码服务
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
Huawei ENSP simulator enables devices of multiple routers to access each other
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
数十亿公民信息遭泄漏!公有云上的数据安全还有“救”吗?
企业数字化转型最佳实践案例:基于云的数字化平台系统安全措施简介与参考
D3.js+Three.js数据可视化3d地球js特效
Nmap scan
Play the music of youth
Jmeter 之压测入门
Hands on deep learning (III) -- convolutional neural network CNN
