Find Nearest Point That Has the Same X or Y Coordinate LeetCode solution in C++
1class Solution {2public:3int nearestValidPoint(int x, int y, vector<vector<int>> &points) {4int pos = -1;5int ans = INT_MAX;67for (int i = 0; i < points.size(); i++) {8if (points[i][0] == x or points[i][1] == y) {9int dist = abs(x - points[i][0]) + abs(y - points[i][1]);10if (dist < ans) {11pos = i;12ans = dist;13}14}15}16return pos;17}18};