0973.cpp (600B)
1 class Solution { 2 typedef pair<double, int> pdi; 3 4 public: 5 vector<vector<int>> kClosest(vector<vector<int>> &points, int k) { 6 auto cmp = [](const pdi &a, const pdi &b) { return a.first < b.first; }; 7 priority_queue<pdi, vector<pdi>, decltype(cmp)> pq(cmp); 8 for (int i = 0; i < points.size(); i++) { 9 pq.push({sqrt(pow(points[i][0], 2) + pow(points[i][1], 2)), i}); 10 if (pq.size() > k) pq.pop(); 11 } 12 13 vector<vector<int>> res(k); 14 while (k--) 15 res[k] = points[pq.top().second], pq.pop(); 16 return res; 17 } 18 };