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

你两个整数 x 和 y ,表示你在一个笛卡尔坐标系下的 (x, y) 处。同时,在同一个坐标系下给你一个数组 points ,其中 points[i] = [ai, bi] 表示在 (ai, bi) 处有一个点。当一个点与你所在的位置有相同的 x 坐标或者相同的 y 坐标时,我们称这个点是 有效的 。

请返回距离你当前位置 曼哈顿距离 最近的 有效 点的下标(下标从 0 开始)。如果有多个最近的有效点,请返回下标 最小 的一个。如果没有有效点,请返回 -1 。

两个点 (x1, y1) 和 (x2, y2) 之间的 曼哈顿距离 为 abs(x1 - x2) + abs(y1 - y2)
示例 1:

输入:x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]
输出:2
解释:所有点中,[3,1],[2,4] 和 [4,4] 是有效点。有效点中,[2,4] 和 [4,4] 距离你当前位置的曼哈顿距离最小,都为 1 。[2,4] 的下标最小,所以返回 2 。

这题只要一个循环就能做所有的事,比如判断是否是有效点,是否是距离最近的。因为是从0开始遍历的,就不用再判断相同距离那个索引小。

lass Solution {
public:
    int nearestValidPoint(int x, int y, vector<vector<int>>& points) {
        int res = 0;
        int min_sum = INT_MAX, index = 0, cnt = 0;
        
        for (int i = 0; i < points.size(); i ++ )           //遍历每个坐标
        {
            if (points[i][0] == x || points[i][1] == y)     //找到有效坐标
            {
                cnt ++ ;                                    //cnt表示有效坐标个数
                if (abs(points[i][0] - x) + abs(points[i][1] - y) < min_sum)
                {
                    min_sum = abs(points[i][0] - x) + abs(points[i][1] - y);    //更新最小曼哈顿距离
                    index = i;            //因为是下标按顺序遍历,故不需要判断小标大小,小的肯定先遍历到
                }
            }
        }
        
        if (!cnt) return -1;                                //不存在有效坐标,返回0
        return index;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C++实现一组的x或y坐标相同,对应的y或x坐标的和等于一个固定的例子: ```c++ #include <iostream> #include <unordered_map> #include <vector> using namespace std; vector<pair<int, int>> findPairs(int arr[][2], int n, int sum, bool isXCoord) { vector<pair<int, int>> result; unordered_map<int, int> hash; for (int i = 0; i < n; i++) { int coord = isXCoord ? arr[i][0] : arr[i][1]; int otherCoord = sum - coord; if (hash.find(otherCoord) != hash.end()) { // Found matching coordinate int count = hash[otherCoord]; for (int j = 0; j < count; j++) { // Add all matching coordinates result.push_back(isXCoord ? make_pair(i, hash[otherCoord + j]) : make_pair(hash[otherCoord + j], i)); } } hash[coord]++; } return result; } int main() { int arr[][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}; int n = sizeof(arr) / sizeof(arr[0]); int sum = 10; vector<pair<int, int>> xMatches = findPairs(arr, n, sum, true); vector<pair<int, int>> yMatches = findPairs(arr, n, sum, false); cout << "X Matches:\n"; for (auto& p : xMatches) { cout << "(" << arr[p.first][0] << ", " << arr[p.first][1] << ") and (" << arr[p.second][0] << ", " << arr[p.second][1] << ")\n"; } cout << "\nY Matches:\n"; for (auto& p : yMatches) { cout << "(" << arr[p.first][0] << ", " << arr[p.first][1] << ") and (" << arr[p.second][0] << ", " << arr[p.second][1] << ")\n"; } return 0; } ``` 这个程序首先实现了一个用于查找坐标对的函数`findPairs`,它接受一个二维数组,数组大小,一个固定的,以及一个指示要匹配x坐标或y坐标的布尔。该函数使用一个哈希表来存储已经访问的坐标,然后遍历数组中的每个坐标,检查是否存在匹配的坐标。如果找到了匹配的坐标,它会将所有匹配的坐标对添加到一个结果向量中。 主函数中,我们首先定义了一个包含坐标的二维数组和一个固定的。然后,我们调用`findPairs`函数两次,一次用于匹配x坐标,一次用于匹配y坐标。最后,我们循环遍历每个匹配的坐标对,并将它们打印到控制台上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值