My java solution


  • 0
    C
    import java.util.*;
    
    public class Solution {
    public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
       
       double [][] d = new double[5][5];
       
       int [][] points = new int[5][5];
       
       points[1] = p1;
       points[2] = p2;
       points[3] = p3;
       points[4] = p4;
       
       Hashtable <Double, Integer> table = new Hashtable<Double, Integer>();
       
       for(int i=1;i<5;i++)
        {   
            for(int j=1;j<5;j++)
            {   
                d[i][j] = getDistance(points[i],points[j]);
                table.put(new Double(d[i][j]),1);
            }
            
            if(table.size()!=3)
            {
                return false;
            }
            
            else
            {
                table = new Hashtable<Double, Integer>();
            }
        }
        
        int e1 = 0;
        int e2 = 0;
        int e3 = 0;
        
        for(int i=1;i<5;i++)
        {
            if(d[i][1]+d[i][2]+d[i][3] == d[i][4] || d[i][1]+d[i][2]+d[i][4] == d[i][3] || d[i][1]+d[i][3]+d[i][4] == d[i][2] || d[i][2]+d[i][3]+d[i][4] == d[i][1])
            {
                continue;
            }
            else
            {
                return false;
            }
        }
        
        return true;
    }
    
    public double getDistance(int [] p1, int [] p2)
    {
        return Math.pow(p1[0]-p2[0],2)+Math.pow(p1[1]-p2[1],2);
    }
    

    }


Log in to reply
 

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