0%

[題解]Leetcode 973 K Closest Points to Origin

973 K Closest Points to Origin

題目連結

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
int n = points.size();
multimap<int,int> mp;
for(int i = 0;i < n;i++){
int x = points[i][0];
int y = points[i][1];
mp.insert({x * x + y * y,i});
}
auto it = mp.begin();
vector<vector<int>> ans;
for(int i=0;i<k;i++){
int id = it->second;
ans.push_back(points[id]);
it++;
}
return ans;
}
};