8000 add cpp solution · xulongjun/Leetcode@f7f14a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit f7f14a0

Browse files
committed
add cpp solution
1 parent 7e889f7 commit f7f14a0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
struct T {
4+
int x, y, d;
5+
T(){}
6+
T(int x, int y): x(x), y(y){d = x * x + y * y;}
7+
bool operator < (const T& tmp) const {
8+
return d < tmp.d;
9+
}
10+
};
11+
vector<vector<int>> kClosest(vector<vector<int>>& points, int K) {
12+
priority_queue<T> q;
13+
for (int i = 0; i < points.size(); i++){
14+
if (q.size() == K){
15+
if (q.top().d > points[i][0] * points[i][0] + points[i][1] * points[i][1]) {
16+
q.pop();
17+
q.push(T(points[i][0], points[i][1]));
18+
}
19+
}
20+
else
21+
q.push(T(points[i][0], points[i][1]));
22+
}
23+
24+
vector<vector<int>> ans;
25+
while(!q.empty()){
26+
vector<int> v = {q.top().x, q.top().y};
27+
ans.push_back(v);
28+
q.pop();
29+
}
30+
return ans;
31+
}
32+
};

0 commit comments

Comments
 (0)
0