C++ verbose But easy to understand code


  • 0
    H
    bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
        int l2, l3, l4;
        vector<int> s1, s2, di;
        
        p2[0] = p1[0] - p2[0];
        p2[1] = p1[1] - p2[1];
        
        p3[0] = p1[0] - p3[0];
        p3[1] = p1[1] - p3[1];
        
        p4[0] = p1[0] - p4[0];
        p4[1] = p1[1] - p4[1];
        
        l2 = p2[0]*p2[0] + p2[1]*p2[1];
        l3 = p3[0]*p3[0] + p3[1]*p3[1];
        l4 = p4[0]*p4[0] + p4[1]*p4[1];
        
        if(l2 != l3 && l3 != l4 && l4 != l2) return false;
        if(l2 == l3 && l3 == l4) return false;
        
        if(l2 == l3){s1 = p2; s2 = p3; di = p4;}
        if(l3 == l4){s1 = p3; s2 = p4; di = p2;}
        if(l4 == l2){s1 = p2; s2 = p4; di = p3;}
        
        return s1[0] + s2[0] == di[0] && s1[1] + s2[1] == di[1] && s1[0]*s2[0] + s1[1]*s2[1] == 0;
    }

Log in to reply
 

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