593. Valid Square
https://leetcode.com/problems/valid-square/
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: True/*
* @lc app=leetcode id=593 lang=cpp
*
* [593] Valid Square
*/
// @lc code=start
class Solution {
public:
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
const auto dist = [](vector<int> &a, vector<int> &b) {
return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]);
};
vector<vector<int>> v({p1, p2, p3, p4});
unordered_set<int> s;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < i; ++j) {
s.insert(dist(v[i], v[j]));
}
}
return s.size() == 2 && !s.count(0);
}
};
// @lc code=end
Last updated