我有以下问题:给定点坐标列表,将k最接近点返回到原点,返回类型应为坐标列表。 我已经处理了这段代码,但需要修复返回类型smallestkpoints,以匹配最近点方法的返回类型。 有什么想法吗?
import java.lang.Math;
import java.util.List;
import java.util.Arrays;
import java.util.Hashtable;
import java.lang.Integer;
import java.lang.Double;
public class ClosestKPoints {
//distance to origin
public static Double distance(Integer x, Integer y) {
double powX = Math.pow(x, 2);
double powY = Math.pow(y, 2);
return (Double)(Math.sqrt(powX + powY));
}
public static List<List<Integer>> closestPoints(int k, List<List<Integer>> points, int all) {
Hashtable<Double,List<Integer>> pt = new Hashtable();
Double [] dt = new Double[all];//where we store distances later to be sorted
List<List<Integer>> smallestKPoints;//to be used as return result
for (int i = 0; i <points.size(); i++) {
List<Integer> in = points.get(i);
for (int j = 0; j < in.size() - 1; j++) {
Integer x = in.get(j);
Integer y = in.get(j + 1);
pt.put(distance( x, y), points.get(i));
dt[i] = distance(x, y);
}
}
Arrays.sort(dt);
for (int n = 0; n < k; n++) {
List<Integer> smallestKPoints = pt.get(dt[n]);
}
return smallestKPoints;
}
}