Problem: 1200. 最小绝对差
思路
先排序(用sort),找出最小绝对差,然后再次遍历添加数组
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
vector<vector<int>>res;
sort(arr.begin(),arr.end());
int mi=arr[1]-arr[0],n=arr.size();
if(n==2){
res.push_back(arr);
return res;
}
for(int i=0;i<n-1;i++){
if(arr[i+1]-arr[i]<mi)mi=(arr[i+1]-arr[i]);
}
for(int i=0;i<n-1;i++){
if(arr[i+1]-arr[i]==mi){
vector<int>l;
l.push_back(arr[i]);
l.push_back(arr[i+1]);
res.push_back(l);
}
}
return res;
}
};
标签:arr,int,res,1200,back,力扣,vector,最小,push
From: https://blog.51cto.com/u_15974747/8776802