码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode-5197 Minimum Absolute Difference(最小绝对差)

时间:2019-09-22 12:44:47      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:define   vector   begin   最小   absolute   ini   leetcode   ++   push   

 1 #define _for(i,a,b) for(int i = (a);i < b;i ++)
 2 #define _rep(i,a,b) for(int i = (a);i > b;i --)
 3 #define INF 0x3f3f3f3f
 4 #define MOD 1000000007
 5 #define pb push_back
 6 #define maxn 10003
 7 
 8 class Solution
 9 {
10     public:
11         vector<vector<int>> minimumAbsDifference(vector<int>& arr)
12         {
13             vector<vector<int>> ans;
14             sort(arr.begin(),arr.end());
15             int diff = INF;
16             _for(i,0,arr.size()-1)
17                 diff = min(diff,arr[i+1]-arr[i]);
18             
19             vector<int> tmp;
20             _for(i,0,arr.size()-1)
21             {
22                 tmp.clear();
23                 if(arr[i+1]-arr[i]==diff)
24                 {
25                     tmp.pb(arr[i]);
26                     tmp.pb(arr[i+1]);
27                     ans.pb(tmp);
28                 }
29             }
30             return ans;
31         }
32 };

 

Leetcode-5197 Minimum Absolute Difference(最小绝对差)

标签:define   vector   begin   最小   absolute   ini   leetcode   ++   push   

原文地址:https://www.cnblogs.com/Asurudo/p/11566936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!