一些格式
empty 2d array: new int[0][]; “\t\t” import static java.lang.System.*
floor() 对一个数向下取整
比较器
1030 Matrix Cells in Distance Order 格式为: Arrays.sort(target, (s1, s2) -> {expression});
class Solution {
public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
int[][] res = new int[rows*cols][2];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int index = i*cols + j;
res[index][0] = i;
res[index][1] = j;
}
}
Arrays.sort(res, (arr1, arr2) -> {
int dis1 = dis(arr1[0], arr1[1], rCenter, cCenter);
int dis2 = dis(arr2[0], arr2[1], rCenter, cCenter);
return Integer.compare(dis1, dis2);}
);
return res;
}
private int dis (int x1, int y1, int x2, int y2) {
return Math.abs(x1-x2) + Math.abs(y1-y2);
}
}
MinPQ in java
|