Skip to content

Commit 7436fdf

Browse files
committed
feat: add solutions to lc problem: No.2103
No.2013.Detect Squares
1 parent 1565dea commit 7436fdf

File tree

5 files changed

+316
-2
lines changed

5 files changed

+316
-2
lines changed

solution/2000-2099/2013.Detect Squares/README.md

+108-1
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,129 @@ detectSquares.count([11, 10]); // 返回 2 。你可以选择:
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
哈希表实现。
67+
6668
<!-- tabs:start -->
6769

6870
### **Python3**
6971

7072
<!-- 这里可写当前语言的特殊实现逻辑 -->
7173

7274
```python
73-
75+
class DetectSquares:
76+
77+
def __init__(self):
78+
self.mp = defaultdict(Counter)
79+
80+
def add(self, point: List[int]) -> None:
81+
x, y = point
82+
self.mp[x][y] += 1
83+
84+
def count(self, point: List[int]) -> int:
85+
x, y = point
86+
ans = 0
87+
if x not in self.mp:
88+
return ans
89+
xcnt = self.mp[x]
90+
91+
for x1, counter in self.mp.items():
92+
if x1 != x:
93+
d = x1 - x
94+
ans += xcnt[y + d] * counter[y] * counter[y + d]
95+
ans += xcnt[y - d] * counter[y] * counter[y - d]
96+
return ans
7497
```
7598

7699
### **Java**
77100

78101
<!-- 这里可写当前语言的特殊实现逻辑 -->
79102

80103
```java
104+
class DetectSquares {
105+
private Map<Integer, Map<Integer, Integer>> mp = new HashMap<>();
106+
107+
public DetectSquares() {
108+
109+
}
110+
111+
public void add(int[] point) {
112+
int x = point[0], y = point[1];
113+
if (!mp.containsKey(x)) {
114+
mp.put(x, new HashMap<>());
115+
}
116+
mp.get(x).put(y, mp.get(x).getOrDefault(y, 0) + 1);
117+
}
118+
119+
public int count(int[] point) {
120+
int x = point[0], y = point[1];
121+
int ans = 0;
122+
if (!mp.containsKey(x)) {
123+
return ans;
124+
}
125+
Map<Integer, Integer> xcnt = mp.get(x);
126+
for (Map.Entry<Integer, Map<Integer, Integer>> e : mp.entrySet()) {
127+
int x1 = e.getKey();
128+
Map<Integer, Integer> counter = e.getValue();
129+
if (x1 != x) {
130+
int d = x1 - x;
131+
ans += xcnt.getOrDefault(y + d, 0) * counter.getOrDefault(y, 0) * counter.getOrDefault(y + d, 0);
132+
ans += xcnt.getOrDefault(y - d, 0) * counter.getOrDefault(y, 0) * counter.getOrDefault(y - d, 0);
133+
}
134+
}
135+
return ans;
136+
}
137+
}
138+
139+
/**
140+
* Your DetectSquares object will be instantiated and called as such:
141+
* DetectSquares obj = new DetectSquares();
142+
* obj.add(point);
143+
* int param_2 = obj.count(point);
144+
*/
145+
```
81146

147+
### **C++**
148+
149+
```cpp
150+
class DetectSquares {
151+
public:
152+
unordered_map<int, unordered_map<int, int>> mp;
153+
154+
DetectSquares() {
155+
156+
}
157+
158+
void add(vector<int> point) {
159+
int x = point[0], y = point[1];
160+
++mp[x][y];
161+
}
162+
163+
int count(vector<int> point) {
164+
int x = point[0], y = point[1];
165+
int ans = 0;
166+
if (!mp.count(x)) return ans;
167+
auto xcnt = mp[x];
168+
for (auto e : mp)
169+
{
170+
int x1 = e.first;
171+
auto counter = e.second;
172+
if (x1 != x)
173+
{
174+
int d = x1 - x;
175+
ans += xcnt[y + d] * counter[y] * counter[y + d];
176+
ans += xcnt[y - d] * counter[y] * counter[y - d];
177+
}
178+
}
179+
return ans;
180+
}
181+
};
182+
183+
/**
184+
* Your DetectSquares object will be instantiated and called as such:
185+
* DetectSquares* obj = new DetectSquares();
186+
* obj->add(point);
187+
* int param_2 = obj->count(point);
188+
*/
82189
```
83190
84191
### **...**

solution/2000-2099/2013.Detect Squares/README_EN.md

+106-1
Original file line numberDiff line numberDiff line change
@@ -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
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class DetectSquares {
2+
public:
3+
unordered_map<int, unordered_map<int, int>> mp;
4+
5+
DetectSquares() {
6+
7+
}
8+
9+
void add(vector<int> point) {
10+
int x = point[0], y = point[1];
11+
++mp[x][y];
12+
}
13+
14+
int count(vector<int> point) {
15+
int x = point[0], y = point[1];
16+
int ans = 0;
17+
if (!mp.count(x)) return ans;
18+
auto xcnt = mp[x];
19+
for (auto e : mp)
20+
{
21+
int x1 = e.first;
22+
auto counter = e.second;
23+
if (x1 != x)
24+
{
25+
int d = x1 - x;
26+
ans += xcnt[y + d] * counter[y] * counter[y + d];
27+
ans += xcnt[y - d] * counter[y] * counter[y - d];
28+
}
29+
}
30+
return ans;
31+
}
32+
};
33+
34+
/**
35+
* Your DetectSquares object will be instantiated and called as such:
36+
* DetectSquares* obj = new DetectSquares();
37+
* obj->add(point);
38+
* int param_2 = obj->count(point);
39+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class DetectSquares {
2+
private Map<Integer, Map<Integer, Integer>> mp = new HashMap<>();
3+
4+
public DetectSquares() {
5+
6+
}
7+
8+
public void add(int[] point) {
9+
int x = point[0], y = point[1];
10+
if (!mp.containsKey(x)) {
11+
mp.put(x, new HashMap<>());
12+
}
13+
mp.get(x).put(y, mp.get(x).getOrDefault(y, 0) + 1);
14+
}
15+
16+
public int count(int[] point) {
17+
int x = point[0], y = point[1];
18+
int ans = 0;
19+
if (!mp.containsKey(x)) {
20+
return ans;
21+
}
22+
Map<Integer, Integer> xcnt = mp.get(x);
23+
for (Map.Entry<Integer, Map<Integer, Integer>> e : mp.entrySet()) {
24+
int x1 = e.getKey();
25+
Map<Integer, Integer> counter = e.getValue();
26+
if (x1 != x) {
27+
int d = x1 - x;
28+
ans += xcnt.getOrDefault(y + d, 0) * counter.getOrDefault(y, 0) * counter.getOrDefault(y + d, 0);
29+
ans += xcnt.getOrDefault(y - d, 0) * counter.getOrDefault(y, 0) * counter.getOrDefault(y - d, 0);
30+
}
31+
}
32+
return ans;
33+
}
34+
}
35+
36+
/**
37+
* Your DetectSquares object will be instantiated and called as such:
38+
* DetectSquares obj = new DetectSquares();
39+
* obj.add(point);
40+
* int param_2 = obj.count(point);
41+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class DetectSquares:
2+
3+
def __init__(self):
4+
self.mp = defaultdict(Counter)
5+
6+
def add(self, point: List[int]) -> None:
7+
x, y = point
8+
self.mp[x][y] += 1
9+
10+
def count(self, point: List[int]) -> int:
11+
x, y = point
12+
ans = 0
13+
if x not in self.mp:
14+
return ans
15+
xcnt = self.mp[x]
16+
17+
for x1, counter in self.mp.items():
18+
if x1 != x:
19+
d = x1 - x
20+
ans += xcnt[y + d] * counter[y] * counter[y + d]
21+
ans += xcnt[y - d] * counter[y] * counter[y - d]
22+
return ans

0 commit comments

Comments
 (0)