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

Golang中UTF编码和字符集

福昕PDF编辑器v10.1.8绿色版

数十亿公民信息遭泄漏!公有云上的数据安全还有“救”吗?
![[1200. Différence absolue minimale]](/img/fa/4ffbedd8f24c75a20d3eaeaf0430ae.png)
[1200. Différence absolue minimale]

Solution of 5g unstable 5g signal often dropped in NetWare r7000 Merlin system

Leetcode+ 81 - 85 monotone stack topic

Advantages of RFID warehouse management system solution

TweenMax表情按钮js特效

UTF encoding and character set in golang

Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t
随机推荐
Implementation of redis distributed lock
ApplicationContext 与 BeanFactory 区别(MS)
接口設計時的一些建議
华为模拟器ensp常用命令
admas零件名重复
华为ensp模拟器实现通信安全(交换机)
【optimtool.unconstrain】无约束优化工具箱
记一次重复造轮子(Obsidian 插件设置说明汉化)
华为ensp模拟器 给路由器配置DHCP
LeetCode 8. String conversion integer (ATOI)
Alibaba testers use UI automated testing to achieve element positioning
acwing 3302. 表达式求值
Introduction to pressure measurement of JMeter
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
[solution] paddlepaddle 2 X call static graph mode
render函数与虚拟dom
MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
js 3D爆炸碎片图片切换js特效
数十亿公民信息遭泄漏!公有云上的数据安全还有“救”吗?
Go language notes (4) go common management commands
