Share my very easy understood Java solution using HashSet


  • 0
    J
    public class Solution {
        public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
            double a1 = Math.sqrt( (p1[0]-p2[0])*(p1[0]-p2[0]) + (p1[1]-p2[1])*(p1[1]-p2[1]));
            double a2 = Math.sqrt( (p1[0]-p3[0])*(p1[0]-p3[0]) + (p1[1]-p3[1])*(p1[1]-p3[1]));
            double a3 = Math.sqrt( (p1[0]-p4[0])*(p1[0]-p4[0]) + (p1[1]-p4[1])*(p1[1]-p4[1]));
            double a4 = Math.sqrt( (p3[0]-p2[0])*(p3[0]-p2[0]) + (p3[1]-p2[1])*(p3[1]-p2[1]));
            double a5 = Math.sqrt( (p4[0]-p2[0])*(p4[0]-p2[0]) + (p4[1]-p2[1])*(p4[1]-p2[1]));
            double a6 = Math.sqrt( (p3[0]-p4[0])*(p3[0]-p4[0]) + (p3[1]-p4[1])*(p3[1]-p4[1]));
            HashSet <Double> set = new HashSet<>();
            set.add(a1);
            set.add(a2);
            set.add(a3);
            set.add(a4);
            set.add(a5);
            set.add(a6);
            if (set.size() == 2){
                for (double i:set){
                    if (i<1)return false;
                }
                return true;
            }
            return false;
        }
    }
    

  • 0
    S

    @JingChen23 said in Share my very easy understood Java solution using HashSet:

    if (i<1)return false;

    In my approach, I am comparing the length of the sides vs the length of diagonal. Can you please explain what you are doing here?


Log in to reply
 

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