Sorting-based 6ms Java Solution


  • 0
    public boolean isReflected(int[][] points) {
    	if (points == null || points.length < 2) {
    		return true;
    	}
    
        // sort:
    	Arrays.sort(points, new Comparator<int[]>() {
    		@Override
    		public int compare(int[] arr1, int[] arr2) {
    			if (arr1[0] < arr2[0]) {
    				return -1;
    			} else if (arr1[0] > arr2[0]) {
    				return 1;
    			} else {
    				if (arr1[1] < arr2[1]) {
    					return -1;
    				} else if (arr1[1] > arr2[1]) {
    					return 1;
    				} else {
    					return 0;
    				}
    			}
    		}
    	});
    
    	int n = points.length;
    
        // sort again the 2nd half:
    	Arrays.sort(points, ((n - 1) >> 1) + 1, n, new Comparator<int[]>() {
    
    		@Override
    		public int compare(int[] arr1, int[] arr2) {
    			if (arr1[0] < arr2[0]) {
    				return -1;
    			} else if (arr1[0] > arr2[0]) {
    				return 1;
    			} else {
    				if (arr1[1] < arr2[1]) {
    					return 1;
    				} else if (arr1[1] > arr2[1]) {
    					return -1;
    				} else {
    					return 0;
    				}
    			}
    		}
    
    	});
    
        // compare:
    	int left = n >> 1, right = n >> 1;
    	if ((n & 1) == 0) {
    		left--;
    	}
    	int midX2 = points[left][0] + points[right][0];
    
    	while (left >= 0) {
    		if (points[left][0] != points[right][0]) {
    			if (points[left][0] + points[right][0] != midX2) {
    				return false;
    			} else if (points[left][1] != points[right][1]) {
    				return false;
    			}
    		}
    
    		left--;
    		right++;
    	}
    
    	return true;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.