// 1779. Find Nearest Point That Has the Same X or Y Coordinate
class Solution {
public int nearestValidPoint(int x, int y, int[][] points) {
int minManhattanDistance = Integer.MAX_VALUE;
int ans = -1;
for (int i = 0; i < points.length; ++i) {
if (points[i][0] == x || points[i][1] == y) {
int manhattanDistance = Math.abs(points[i][0] - x) + Math.abs(points[i][1] - y);
if (manhattanDistance < minManhattanDistance) {
minManhattanDistance = manhattanDistance;
ans = i;
}
}
}
return ans;
}
}
学习笔记:
本月的第一题,必然是简单题。
这道题是枚举算法把所有坐标点都计算出来求曼哈顿距离。