Find Nearest Point That Has the Same X or Y Coordinate LeetCode solution in Java
1class Solution {2public int nearestValidPoint(int x, int y, int[][] points) {3int min = Integer.MAX_VALUE, index = -1, i;45for (i = 0; i < points.length; i++) {6if (x == points[i][0] || y == points[i][1]) {7int d = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]);8if (d < min) {9min = d;10index = i;11}12}13}14// if ( min== Integer.MAX_VALUE) return -1; --> no longer needed as index is initialized as -115// in the declartion.16return index;17}18}