8000 Add Java solution for day30: K Closest Points to Origin · xulongjun/Leetcode@99e14f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99e14f2

Browse files
committed
Add Java solution for day30: K Closest Points to Origin
1 parent 31c9988 commit 99e14f2

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int[][] kClosest(int[][] points, int K) {
3+
int[][] res = new int[K][2];
4+
PriorityQueue<int[]> minHeap = new PriorityQueue<>
5+
((a, b) -> (a[0]*a[0] + a[1]*a[1]) - (b[0]*b[0] + b[1]*b[1]));
6+
7+
for (int[] point : points) {
8+
minHeap.add(point);
9+
}
10+
11+
for (int i = 0; i < K; i++) {
12+
res[i] = minHeap.poll();
13+
14+
}
15+
return res;
16+
}
17+
}

0 commit comments

Comments
 (0)
0