Java simple solution, O(n)


  • 0
    import java.awt.Point;
    public class Solution {
        public boolean isReflected(int[][] points) {
            int min = Integer.MAX_VALUE;
            int max = Integer.MIN_VALUE;
            //find the min and max on X-axis, the x value of the middle axis should be (min + max) / 2;
            HashSet<Point> set = new HashSet<Point>();
            for(int[] arr : points) {
                set.add(new Point(arr[0], arr[1]));
                min = Math.min(min, arr[0]);
                max = Math.max(max, arr[0]);
            }
            for(int[] arr : points){
                // check if there's a point on the other side taking (min + max) / 2 as axis
                if(!set.contains(new Point(min + max - arr[0], arr[1]))){
                    return false;
                }
            }
            return true;
        }
    }
    

Log in to reply
 

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