@@ -62,13 +62,118 @@ detectSquares.count([11, 10]); // return 2. You can choose:
6262### ** Python3**
6363
6464``` python
65-
65+ class DetectSquares :
66+
67+ def __init__ (self ):
68+ self .mp = defaultdict(Counter)
69+
70+ def add (self , point : List[int ]) -> None :
71+ x, y = point
72+ self .mp[x][y] += 1
73+
74+ def count (self , point : List[int ]) -> int :
75+ x, y = point
76+ ans = 0
77+ if x not in self .mp:
78+ return ans
79+ xcnt = self .mp[x]
80+
81+ for x1, counter in self .mp.items():
82+ if x1 != x:
83+ d = x1 - x
84+ ans += xcnt[y + d] * counter[y] * counter[y + d]
85+ ans += xcnt[y - d] * counter[y] * counter[y - d]
86+ return ans
6687```
6788
6889### ** Java**
6990
7091``` java
92+ class DetectSquares {
93+ private Map<Integer , Map<Integer , Integer > > mp = new HashMap<> ();
94+
95+ public DetectSquares () {
96+
97+ }
98+
99+ public void add (int [] point ) {
100+ int x = point[0 ], y = point[1 ];
101+ if (! mp. containsKey(x)) {
102+ mp. put(x, new HashMap<> ());
103+ }
104+ mp. get(x). put(y, mp. get(x). getOrDefault(y, 0 ) + 1 );
105+ }
106+
107+ public int count (int [] point ) {
108+ int x = point[0 ], y = point[1 ];
109+ int ans = 0 ;
110+ if (! mp. containsKey(x)) {
111+ return ans;
112+ }
113+ Map<Integer , Integer > xcnt = mp. get(x);
114+ for (Map . Entry<Integer , Map<Integer , Integer > > e : mp. entrySet()) {
115+ int x1 = e. getKey();
116+ Map<Integer , Integer > counter = e. getValue();
117+ if (x1 != x) {
118+ int d = x1 - x;
119+ ans += xcnt. getOrDefault(y + d, 0 ) * counter. getOrDefault(y, 0 ) * counter. getOrDefault(y + d, 0 );
120+ ans += xcnt. getOrDefault(y - d, 0 ) * counter. getOrDefault(y, 0 ) * counter. getOrDefault(y - d, 0 );
121+ }
122+ }
123+ return ans;
124+ }
125+ }
126+
127+ /**
128+ * Your DetectSquares object will be instantiated and called as such:
129+ * DetectSquares obj = new DetectSquares();
130+ * obj.add(point);
131+ * int param_2 = obj.count(point);
132+ */
133+ ```
71134
135+ ### ** C++**
136+
137+ ``` cpp
138+ class DetectSquares {
139+ public:
140+ unordered_map<int, unordered_map<int, int>> mp;
141+
142+ DetectSquares() {
143+
144+ }
145+
146+ void add (vector<int > point) {
147+ int x = point[ 0] , y = point[ 1] ;
148+ ++mp[ x] [ y ] ;
149+ }
150+
151+ int count(vector<int> point) {
152+ int x = point[0], y = point[1];
153+ int ans = 0;
154+ if (!mp.count(x)) return ans;
155+ auto xcnt = mp[x];
156+ for (auto e : mp)
157+ {
158+ int x1 = e.first;
159+ auto counter = e.second;
160+ if (x1 != x)
161+ {
162+ int d = x1 - x;
163+ ans += xcnt[y + d] * counter[y] * counter[y + d];
164+ ans += xcnt[y - d] * counter[y] * counter[y - d];
165+ }
166+ }
167+ return ans;
168+ }
169+ };
170+
171+ /**
172+ * Your DetectSquares object will be instantiated and called as such:
173+ * DetectSquares* obj = new DetectSquares();
174+ * obj->add(point);
175+ * int param_2 = obj->count(point);
176+ * /
72177```
73178
74179### **...**
0 commit comments