下面的代码:
List<List<Integer>> res = new ArrayList<>();
List<Integer> row = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
row.add(i);
res.add(row);
}
Run Code Online (Sandbox Code Playgroud)
资源: [ [1,2,3],[1,2,3] ,[1,2,3]]
是这样写的:
for (int i = 1; i <= 3; i++) {
row.add(i);
res.add(new ArrayList<>(row));
}
Run Code Online (Sandbox Code Playgroud)
资源: [ [1],[1,2] ,[1,2,3]]
给定一个包含N个点的数组,找到2D平面中最接近原点(0,0)的K个点。您可以假设K比N小得多,并且N非常大。
例如:
Run Code Online (Sandbox Code Playgroud)given array: (1,0), (3,0), (2,0), K = 2 Result = (1,0), (2,0)(结果应按距离升序排列)
码:
import java.util.*;
class CPoint {
double x;
double y;
public CPoint(double x, double y) {
this.x = x;
this.y = y;
}
}
public class KClosest {
/**
* @param myList: a list of myList
* @param k: the number of closest myList
* @return: the k closest myList
*/
public static CPoint[] getKNearestPoints(CPoint[] myList, int k) {
if (k <= 0 || k …Run Code Online (Sandbox Code Playgroud)