1779. 找到最近的有相同 X 或 Y 坐标的点

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;
    }
}
posted @ 2022-12-01 09:00  Eiffelzero  阅读(40)  评论(0)    收藏  举报