码迷,mamicode.com
首页 > 编程语言 > 详细

【数组】面试题 16.06. 最小差

时间:2020-05-05 20:11:14      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:排序   cto   strong   img   info   http   com   数组   str   

题目:

技术图片

 

 

解答:

先排序,然后设定返回值为最大,用双指针求得结果。

 1 class Solution {
 2 public:
 3     int smallestDifference(vector<int>& a, vector<int>& b) 
 4     {
 5         sort(a.begin(),a.end());
 6         sort(b.begin(),b.end());
 7 
 8         long ret = INT_MAX;
 9 
10         for(int i = 0, j = 0; i < a.size() && j < b.size();)
11         {
12             ret = min(ret,abs(long(a[i])-long(b[j])));
13             if(a[i] < b[j])
14             {
15                 i++;
16             }
17             else
18             {
19                 j++;
20             }
21         }
22         return ret;
23     }
24 };

 

【数组】面试题 16.06. 最小差

标签:排序   cto   strong   img   info   http   com   数组   str   

原文地址:https://www.cnblogs.com/ocpc/p/12832006.html

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