Skip to content

Commit a109a98

Browse files
authored
Create valid-square.cpp
1 parent 1bab7fc commit a109a98

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

C++/valid-square.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
7+
unordered_set<int> s({ d(p1, p2), d(p1, p3),
8+
d(p1, p4), d(p2, p3),
9+
d(p2, p4), d(p3, p4) });
10+
return !s.count(0) && s.size() == 2;
11+
}
12+
13+
private:
14+
int d(vector<int>& p1, vector<int>& p2) {
15+
return (p1[0] - p2[0]) * (p1[0] - p2[0]) +
16+
(p1[1] - p2[1]) * (p1[1] - p2[1]);
17+
}
18+
};

0 commit comments

Comments
 (0)