Java Simple Solution : 9 lines 18 ms faster than 64%


  • 0
    S
     public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
            return checkAngle(p1,p2,p3) && checkAngle(p1,p2,p4) && checkAngle(p1,p3,p4) && checkAngle(p2,p3,p4);
        }
        public int distSq(int[] p1, int[] p2) {
            return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
        }
        public boolean checkAngle (int[] p1, int[] p2, int[] p3) {
            boolean a = distSq(p1,p2) !=0 && (distSq(p1,p2)  == distSq(p2,p3) + distSq (p1,p3)) && distSq(p2,p3) == distSq(p1,p3);
            boolean b = distSq(p1,p3) !=0 && (distSq(p1,p3)  == distSq(p2,p3) + distSq (p1,p2)) && distSq(p2,p3) == distSq(p1,p2);
            boolean c = distSq(p2,p3) !=0 && (distSq(p2,p3)  == distSq(p1,p2) + distSq (p1,p3)) && distSq(p1,p2) == distSq(p1,p3);
            return a || b || c;
        }

Log in to reply
 

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