BiruLyu
6/28/2017 - 5:50 AM

593. Valid Square.java

public class Solution {
    public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
        int[][] points = new int[][] {p1, p2, p3, p4};
        HashMap<Long, Integer> map = new HashMap<Long, Integer>();
        for (int i = 0; i < 4; i++) {
            for (int j = i + 1; j < 4; j++) {
                long temp = (points[i][0] - points[j][0]) * (points[i][0] - points[j][0]) + (points[i][1] - points[j][1]) * (points[i][1] - points[j][1]);
                map.put(temp, map.getOrDefault(temp, 0) + 1);
            }
        }
        return map.size() == 2 && map.containsValue(2) && map.containsValue(4);
    }
}