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
    H

    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]);
            Arrays.sort(lens);
            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
    L

    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
    L

    Sorry! I understand that!My mistake.


Log in to reply
 

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