-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
36 lines (32 loc) · 892 Bytes
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class DetectSquares {
public:
unordered_map<int, unordered_map<int, int>> mp;
DetectSquares() {
}
void add(vector<int> point) {
int x = point[0], y = point[1];
++mp[x][y];
}
int count(vector<int> point) {
int x = point[0], y = point[1];
int ans = 0;
if (!mp.count(x)) return ans;
auto xcnt = mp[x];
for (auto e : mp) {
int x1 = e.first;
auto counter = e.second;
if (x1 != x) {
int d = x1 - x;
ans += xcnt[y + d] * counter[y] * counter[y + d];
ans += xcnt[y - d] * counter[y] * counter[y - d];
}
}
return ans;
}
};
/**
* Your DetectSquares object will be instantiated and called as such:
* DetectSquares* obj = new DetectSquares();
* obj->add(point);
* int param_2 = obj->count(point);
*/