最粗暴的方法,自定义数据结构+重新定义排序规则
class Solution {
public int[][] kClosest(int[][] points, int K) {
int m = points.length;
Node[] nodes = new Node[m];
for(int i=0;i<m;i++){
nodes[i] = new Node(points[i][0],points[i][1]);
}
Arrays.sort(nodes,(l,r)->{
if(l.x * l.x + l.y * l.y > r.x * r.x + r.y * r.y) return 1;
return -1;
});
int[][] res = new int[K][2];
for(int i=0;i<K;i++){
res[i][0] = nodes[i].x;
res[i][1] = nodes[i].y;
}
return res;
}
}
class Node{
public int x;
public int y;
Node(int x,int y){
this.x = x;
this.y = y;
}
}