C++ Solution - handles oblique


  • 0
    class Solution {
    public:
        bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
            vector<vector<int>> v = { p1, p2, p3, p4 };
            sort(v.begin(), v.end());
            for (vector<int> p : v) {
                printf("%d, %d\n", p[0], p[1]);
            }
            
            return nonZeroEqual(distSq(v[0], v[1]), distSq(v[0], v[2]))
                && nonZeroEqual(distSq(v[3], v[1]), distSq(v[3], v[2]))
                && nonZeroEqual(distSq(v[0], v[3]), distSq(v[1], v[2]));
        }
    
    private:
        bool nonZeroEqual(int a, int b) {
            return a && b && a == b;
        }
        int distSq(vector<int>& a, vector<int>& b) {
            int res = pow((a[0] - b[0]), 2) + pow((a[1] - b[1]), 2);
            printf("[%d,%d] - [%d,%d] = %d\n", a[0], a[1], b[0], b[1], res);
            return res;
        }
    };
    

Log in to reply
 

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