JAVA HashSet


  • 0
    G
    public class Solution {
        public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
            Set<Integer> st = new HashSet<>();
            int[][] arr =  {p1,p2,p3,p4};
            for(int i=0;i<3;i++){
                for(int j=i+1;j<=3;j++){
                    st.add(dis(arr[i],arr[j]));
                }
            }
            
            if(st.size()!=2) return false;
            List<Integer> ls = new ArrayList<>();
            for(Integer t:st){
                ls.add(t);
            }
            
            if(ls.get(0)*2==ls.get(1) || ls.get(0)==ls.get(1)*2) return true;
            return false;
        }
        
        public int dis(int[] p1, int[] p2){
            return (int)(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.