Easy + Clean JAVA solution


  • 0
     public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
            int p12 = distance(p1, p2);
            int p13 = distance(p1, p3);
            int p14 = distance(p1, p4);
            if(p12 == 0 || p13 == 0 || p14 == 0) return false;
           
            if((p12 == (2 * p13)) && p13 == p14 ) { //p12 is diagonal distance
                return distance(p2, p3) == distance(p2, p4);
            }
            
            else if((p13 == (2 * p12)) && p12 == p14 ) { //p13 is diagonal distance
                return distance(p4, p3) == distance(p2, p3);
            }
            
            else if((p14 == (2 * p13)) && p13 == p12 ) { //p14 is diagonal distance
                return distance(p2, p4) == distance(p3, p4);
            }
            else return false;
        }
        
        public int distance(int[] p, int[] q) {
            int x = (p[0] - q[0]) * (p[0] - q[0]);
            int y = (p[1] - q[1]) * (p[1] - q[1]);
            return x  + y;
        }
    

Log in to reply
 

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