4 Liner Java


  • 6
    A
    public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
        HashSet<Integer> hs = new HashSet<>(Arrays.asList(dis(p1, p2), dis(p1, p3), dis(p1, p4), dis(p2, p3), dis(p2, p4), dis(p3, p4)));        
    	 return !hs.contains(0) && hs.size()==2; //One each for side & diagonal
        }
        int dis(int[] a, int[] b){
    	 return (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]);
        }
    

  • 1
    A

    Alternatively:

    public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
    
        int[] arr = {dis(p1, p2), dis(p1, p3), dis(p1, p4), dis(p2, p3), dis(p2, p4), dis(p3, p4)};
        Arrays.sort(arr);
             
        return arr[0] > 0 && arr[0] == arr[1] && arr[1] == arr[2] && arr[2] == arr[3] && arr[4] == arr[5];
        }
    	
        int dis(int[] a, int[] b) {
            return (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]);
        }

  • 2
    Y

    @aayushgarg
    it is wrong to judge by size() == 2, as 4 points can be the Equilateral triangle and one point in the middle. It also has 2 same values, the size is 2. But it is triangle not square


  • 0
    A

    @yiminghuang It is not possible to construct an equilateral triangle with vertices on lattice points (a point at the intersection of two or more grid lines). Since we have been given integer array, equilateral triangle case will not exist.
    Proof: https://math.stackexchange.com/questions/105330/equilateral-triangle-whose-vertices-are-lattice-points
    See using: http://triancal.esy.es/?lang=en&a=2&b=2&c=2&A=60°&B=60°


  • 0
    S

    Your code couldn't pass this test case:
    A Parallelogram, 4 sides are equal and also equal to the short diagonal, which could also return true in your code.


  • 1
    A

    @Shinia Unfortunately, if the short diagonal equals the 4 sides, 3 points would also be on an equilateral triangle, which is impossible since the input points are integer array.


  • 0
    M

    The idea is brilliant and the math behind supports the argument that for integer inputs it is IMPOSSIBLE to construct a diamond with an inner angle 60 degrees.
    The code itself it robust.

    I just wrote a stupid one but it is easy to understand.

    public class Solution {
        public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
            HashMap<Integer, Integer> map = new HashMap<>();
            int a12 = getDistanceSquare(p1, p2);
            int a13 = getDistanceSquare(p1, p3);
            int a14 = getDistanceSquare(p1, p4);
            int a23 = getDistanceSquare(p2, p3);
            int a24 = getDistanceSquare(p2, p4);
            int a34 = getDistanceSquare(p3, p4);
            if (a12 == 0 || a13 == 0 || a14 == 0 || a23 == 0 || a24 == 0 || a34 == 0)   return false;
            map.put(a12, map.getOrDefault(a12, 0) + 1);
            map.put(a13, map.getOrDefault(a13, 0) + 1);
            map.put(a14, map.getOrDefault(a14, 0) + 1);
            map.put(a23, map.getOrDefault(a23, 0) + 1);
            map.put(a24, map.getOrDefault(a24, 0) + 1);
            map.put(a34, map.getOrDefault(a34, 0) + 1);
            return map.size() == 2 && (map.get(a12) == 2 || map.get(a12) == 4);
    
        }
        
        private int getDistanceSquare(int[] a, int[] b) {
            return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]);
        }
    }
    

  • 0
    B
    This post is deleted!

Log in to reply
 

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