1779. 找到最近的有相同 X 或 Y 坐标的点
class Solution {
public int nearestValidPoint(int x, int y, int[][] points) {
int n = points.length;
int res = (int)0x3f3f3f3f;
int index = -1;
for(int i = 0; i < n; i ++) {
int x1 = points[i][0], y1 = points[i][1];
if(x1 == x || y1 == y) {
int dis = Math.abs(x - x1) + Math.abs(y - y1);
if(res > dis) {
index = i;
res = dis;
}
}
}
return index;
}
}

浙公网安备 33010602011771号