0593.cpp (687B)
1 class Solution { 2 static int distance(const vector<int> p1, const vector<int> p2) { 3 const int x = p1[0] - p2[0]; 4 const int y = p1[1] - p2[1]; 5 6 return x * x + y * y; 7 } 8 9 public: 10 bool validSquare(const vector<int> &p1, const vector<int> &p2, const vector<int> &p3, 11 const vector<int> &p4) const { 12 if (p1 == p2) return false; 13 14 int d[] = { 15 distance(p1, p2), distance(p1, p3), distance(p1, p4), 16 distance(p2, p3), distance(p2, p4), distance(p3, p4), 17 }; 18 19 sort(d, d + 6); 20 21 return d[0] == d[1] && d[0] == d[2] && d[0] == d[3] && 2 * d[0] == d[4] && 2 * d[0] == d[5]; 22 } 23 };