点击打开链接
class Solution {
public:
struct cmp{
bool operator()(pair<int, int>num1, pair<int, int>num2){
return num1.first +num1.second > num2.first+num2.second;
}
};
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
vector<pair<int, int>> res;
priority_queue< pair<int, int>, vector<pair<int, int> >, cmp> pq; //最小堆
int i=0, j=0, m=nums1.size(), n=nums2.size();
for(i=0; i<m; i++){
for(j=0; j<n;j++){
pq.push(make_pair(nums1[i], nums2[j]));
}
}
while (k>0&&pq.size()>0) {
res.push_back(pq.top());
pq.pop();
k-=1;
}
return res;
}
};