Share my Python solution


  • 0
    H

    I consider using sort to solve the problem, 4 lines should have same length, the other 2 lines have same length

        def validSquare(self, p1, p2, p3, p4):
            """
            :type p1: List[int]
            :type p2: List[int]
            :type p3: List[int]
            :type p4: List[int]
            :rtype: bool
            """
            def fl(p1,p2):
                return (p1[0]-p2[0])** 2 + (p1[1]-p2[1])** 2
                
            length = []
            length.append(fl(p1,p2))
            length.append(fl(p1,p3))
            length.append(fl(p1,p4))
            length.append(fl(p2,p3))
            length.append(fl(p2,p4))
            length.append(fl(p3,p4))
            a = sorted(length)
            
            return len(set(a)) == 2 and len(set(a[:4])) == 1 and len(set(a[4:])) == 1```

Log in to reply
 

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