JS solution


  • 0
    C
    /**
     * @param {number[]} p1
     * @param {number[]} p2
     * @param {number[]} p3
     * @param {number[]} p4
     * @return {boolean}
     */
    var validSquare = function(p1, p2, p3, p4) {
    	d12 = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
    	d13 = (p1[0] - p3[0]) * (p1[0] - p3[0]) + (p1[1] - p3[1]) * (p1[1] - p3[1]);
    	d14 = (p1[0] - p4[0]) * (p1[0] - p4[0]) + (p1[1] - p4[1]) * (p1[1] - p4[1]);
    	d23 = (p2[0] - p3[0]) * (p2[0] - p3[0]) + (p2[1] - p3[1]) * (p2[1] - p3[1]);
    	d34 = (p3[0] - p4[0]) * (p3[0] - p4[0]) + (p3[1] - p4[1]) * (p3[1] - p4[1]);
    	d24 = (p2[0] - p4[0]) * (p2[0] - p4[0]) + (p2[1] - p4[1]) * (p2[1] - p4[1]);
    	var hash = {};
    
    	function setHash(d){
    		if(hash[d]){
    			hash[d] += 1;
    		} else {
    			hash[d] = 1;
    		}
    	}
    	
    	setHash(d12);
    	setHash(d13);
    	setHash(d14);
    	setHash(d23);
    	setHash(d34);
    	setHash(d24);
    
    	if(Object.keys(hash).length !== 2){
    		return false
    	} else{
    		var keys = Object.keys(hash);
    		if(
    			(hash[keys[0]] === 2 && hash[keys[1]] === 4) ||
    			(hash[keys[0]] === 4 && hash[keys[1]] === 2)
    			){
    			return true
    		} else {
    			return false;
    		}
    	}
    };
    

Log in to reply
 

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