标签:排序 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 };
标签:排序 cto strong img info http com 数组 str
原文地址:https://www.cnblogs.com/ocpc/p/12832006.html