Java using HashSet


  • 0
    V
        int getDist(int[] p1, int[] p2)
        {
            int v1 = Math.abs(p1[0] - p2[0]);
            int v2 = Math.abs(p1[1] - p2[1]);
            return v1 * v1 +  v2 * v2;
        }
        public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) 
        {
            HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
            int v1 = getDist(p1, p2), v2 = getDist(p1, p3), v3 = getDist(p1, p4),
            v4 = getDist(p2, p3), v5 = getDist(p2, p4), v6 = getDist(p3, p4);
            hm.put(v1, hm.getOrDefault(v1, 0) + 1);
            hm.put(v2, hm.getOrDefault(v2, 0) + 1);
            hm.put(v3, hm.getOrDefault(v3, 0) + 1);
            hm.put(v4, hm.getOrDefault(v4, 0) + 1);
            hm.put(v5, hm.getOrDefault(v5, 0) + 1);
            hm.put(v6, hm.getOrDefault(v6, 0) + 1);
            return hm.size() == 2 && !hm.containsKey(0);
        }

  • 0
    S

    Shouldn't we check if

    Math.sqrt(2*Math.pow(sides,2) ) == diagonal;
    

Log in to reply
 

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