1779.找到最近的有相同X或Y坐标的点
链接:1779.找到最近的有相同X或Y坐标的点
难度:Easy
标签:数组
简介:请返回距离你当前位置 曼哈顿距离 最近的 有效 点的下标(下标从 0 开始)。
题解 1 - cpp
- 编辑时间:2022-12-01
- 执行用时:132ms
- 内存消耗:57.8MB
- 编程语言:cpp
- 解法介绍:枚举所有点。
class Solution {
public:
int nearestValidPoint(int x, int y, vector<vector<int>>& points) {
int ans = -1, dans = 0x3f3f3f3f;
for (int i = 0; i < points.size(); i++) {
int ix = points[i][0], iy = points[i][1], d = abs(ix - x) + abs(iy - y);
if (ix != x && iy != y) continue;
if (dans > d) ans = i, dans = d;
}
return ans;
}
};