forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValid Square.cpp
More file actions
21 lines (18 loc) · 742 Bytes
/
Valid Square.cpp
File metadata and controls
21 lines (18 loc) · 742 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int dis(vector<int>& p, vector<int>& q){
return pow(p[0]-q[0], 2) + pow(p[1]-q[1], 2);
}
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
vector<int> a;
a.push_back( dis(p1, p2)); // all distance and sort them
a.push_back( dis(p1, p3)); // if it is a valid square then last distance must be dianogal
a.push_back( dis(p1, p4));
a.push_back( dis(p3, p2));
a.push_back( dis(p4, p2));
a.push_back( dis(p4, p3));
sort(a.begin(), a.end());
if(a[0]>0 and a[0]==a[1] and a[1]==a[2] and a[2]==a[3] and a[4]==a[5] and 2*a[2]==a[4] ) return 1;
return 0;
}
};