Group by Y then sort by X, 17ms


  • 0
    J

    easy to understand java solution

    public class Solution {
        public boolean isReflected(int[][] points) {
            Map<Integer, List<Integer>> map = new HashMap<>();
            for (int[] point : points) {
                if (!map.containsKey(point[1])) map.put(point[1], new ArrayList<>());
                map.get(point[1]).add(point[0]);
            }
            Integer pre = null;
            for (List<Integer> list : map.values()) {
                if (list.size() > 2) Collections.sort(list);
                int i = 0, j = list.size() - 1;
                while (i <= j) {
                    int x = list.get(i++) + list.get(j--);
                    if (pre == null) pre = x;
                    else if (x != pre) return false;
                }
            }
            return true;
        }
    }

Log in to reply
 

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