leetcode | 面试题 16.06. 最小差

leetcode | 面试题 16.06. 最小差_第1张图片

class Solution {
public:
    int smallestDifference(vector<int>& a, vector<int>& b) {
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        long min_ = INT_MAX;
        for(int i = 0, j = 0; i < a.size() && j < b.size();)
        {
            min_ = min_ > abs(long(a[i]) - long(b[j])) ? abs(long(a[i]) - long(b[j])) : min_;
            if(a[i] < b[j])
            {
                i++;
            }
            else
            {
                j++;
            }
        }
        return min_;
    }
};

你可能感兴趣的:(leetcode和牛客刷题)