Solution Without using product/multiplication


  • 0
    G
    public class Solution {
    	public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) 
        {
            if(p1[0] == p2[0] && p1[1] == p2[1])
            {
                return false;
            }
            int x0 = p1[0] + p2[0] + p3[0] + p4[0];
            int y0 = p1[1] + p2[1] + p3[1] + p4[1];
            int[][]points = new int[][]{p1, p2, p3, p4};
            for(int[]point : points)
            {
    
            	point[0] = (point[0] << 2) - x0;//moving origin to the center of square
            	point[1] = (point[1] << 2) - y0;//with respect to square center
            }
            int[] iter = p1;
            main:for(int i = 0; i < 3; i++)
            {
            	int nextX = -iter[1]; //rotation 90 degree counter clockwize
            	int nextY = +iter[0];
            	
            	for(int pi = i; pi < points.length; pi++)
            	{
            		if(points[pi][0] == nextX && points[pi][1] == nextY)
            		{
            			iter = points[pi];
            			swap(points, i, pi);
            			continue main;
            		}
            	}
            	return false;
            }
            return true;
    	}
    	static void swap(int[][] arr, int i, int j)
    	{
    		int[] temp = arr[i];
    		arr[i] = arr[j];
    		arr[j] = temp;
    	}
    }
    

Log in to reply
 

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