题目链接:https://leetcode-cn.com/problems/k-closest-points-to-origin/
题目如下:
class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
vector<vector<int>> res;
//默认是less算子也就是返回ab,小的优先级高
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>> minheap;
for(int i=0;i<points.size();i++){
int sum=points[i][0]*points[i][0]+points[i][1]*points[i][1];
minheap.push({sum,i});
}
while(k--){
res.push_back(points[minheap.top().second]);
minheap.pop();
}
return res;
}
};
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)