Kill Process

  • 0

    Click here to see the full article post

  • 0

    We just need to determine if the sides of the qaudrilateral formed by these 4 points are equal. But checking only this won't suffice.

    The second sentence contradicts the first.

  • 0

    @ManuelP I have updated the statement. Hope it seems correct now.

  • 1

    I also consider using sort to solve the problem, not by sorting the four points position but sorting the length of two points. Finally, [a, a, a, a, 2a, 2a] is the correct pattern.

    public class Solution {
        public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
            int[] lens = new int[6];
            lens[0] = (p1[0]-p2[0])*(p1[0]-p2[0])+(p1[1]-p2[1])*(p1[1]-p2[1]);
            lens[1] = (p1[0]-p3[0])*(p1[0]-p3[0])+(p1[1]-p3[1])*(p1[1]-p3[1]);
            lens[2] = (p1[0]-p4[0])*(p1[0]-p4[0])+(p1[1]-p4[1])*(p1[1]-p4[1]);
            lens[3] = (p2[0]-p3[0])*(p2[0]-p3[0])+(p2[1]-p3[1])*(p2[1]-p3[1]);
            lens[4] = (p2[0]-p4[0])*(p2[0]-p4[0])+(p2[1]-p4[1])*(p2[1]-p4[1]);
            lens[5] = (p3[0]-p4[0])*(p3[0]-p4[0])+(p3[1]-p4[1])*(p3[1]-p4[1]);
            for(int i=0; i<3; i++) {
                if (lens[i]==lens[i+1]) continue;
                else return false;
            if (lens[4]!=lens[5]) return false;
            if (lens[0]!=0) return true;
            else return false;

  • 0

    You don't consider the situation that a diamond but its angles are not 90-degree. (For example,it angles are 60-degree,120-degree,60-degree,120-degree,like put two equilateral triangles together). I think your solution should add "the length of four sides != the length of diagonals" in order to avoid this case. And forgive my poor english...

  • 0

    Sorry! I understand that!My mistake.

  • 0

    What I did is check the vectors from the center to four points
    while the length of any one of these vectors must be same and not zero
    and angle among these four vectors must be 90-degree or 180-degree,

    length_Sq > 0 // not a dot
    length_Sq - v[i].x * v[i].x - v[i].y * v[i].y == 0 // same length

    v[i].x * v[j].x + v[i].y * v[j].y == 0 //90-degree:
    (v[i].x + v[j].x == 0) && (v[i].y + v[j].y == 0) // 180-degree

Log in to reply

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