Skip to content

Commit 576638c

Browse files
authored
feat: add solutions to lc problem: No.0750 (#1362)
No.0750.Number Of Corner Rectangles
1 parent 4dcf03f commit 576638c

File tree

7 files changed

+313
-2
lines changed

7 files changed

+313
-2
lines changed

solution/0700-0799/0750.Number Of Corner Rectangles/README.md

+113-1
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,134 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:哈希表 + 枚举**
64+
65+
我们枚举每一行作为矩形的下边,对于当前行,如果列 $i$ 和列 $j$ 都是 $1$,那么我们用哈希表找出此前的所有行中,有多少行的 $i$ 和 $j$ 列都是 $1$,那么就有多少个以 $(i, j)$ 为右下角的矩形,我们将其数量加入答案。然后将 $(i, j)$ 加入哈希表,继续枚举下一对 $(i, j)$。
66+
67+
时间复杂度 $O(m \times n^2)$,空间复杂度 $O(n^2)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
68+
6369
<!-- tabs:start -->
6470

6571
### **Python3**
6672

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

6975
```python
70-
76+
class Solution:
77+
def countCornerRectangles(self, grid: List[List[int]]) -> int:
78+
ans = 0
79+
cnt = Counter()
80+
n = len(grid[0])
81+
for row in grid:
82+
for i, c1 in enumerate(row):
83+
if c1:
84+
for j in range(i + 1, n):
85+
if row[j]:
86+
ans += cnt[(i, j)]
87+
cnt[(i, j)] += 1
88+
return ans
7189
```
7290

7391
### **Java**
7492

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

7795
```java
96+
class Solution {
97+
public int countCornerRectangles(int[][] grid) {
98+
int n = grid[0].length;
99+
int ans = 0;
100+
Map<List<Integer>, Integer> cnt = new HashMap<>();
101+
for (var row : grid) {
102+
for (int i = 0; i < n; ++i) {
103+
if (row[i] == 1) {
104+
for (int j = i + 1; j < n; ++j) {
105+
if (row[j] == 1) {
106+
List<Integer> t = List.of(i, j);
107+
ans += cnt.getOrDefault(t, 0);
108+
cnt.merge(t, 1, Integer::sum);
109+
}
110+
}
111+
}
112+
}
113+
}
114+
return ans;
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
int countCornerRectangles(vector<vector<int>>& grid) {
125+
int n = grid[0].size();
126+
int ans = 0;
127+
map<pair<int, int>, int> cnt;
128+
for (auto& row : grid) {
129+
for (int i = 0; i < n; ++i) {
130+
if (row[i]) {
131+
for (int j = i + 1; j < n; ++j) {
132+
if (row[j]) {
133+
ans += cnt[{i, j}];
134+
++cnt[{i, j}];
135+
}
136+
}
137+
}
138+
}
139+
}
140+
return ans;
141+
}
142+
};
143+
```
144+
145+
### **Go**
146+
147+
```go
148+
func countCornerRectangles(grid [][]int) (ans int) {
149+
n := len(grid[0])
150+
type pair struct{ x, y int }
151+
cnt := map[pair]int{}
152+
for _, row := range grid {
153+
for i, x := range row {
154+
if x == 1 {
155+
for j := i + 1; j < n; j++ {
156+
if row[j] == 1 {
157+
t := pair{i, j}
158+
ans += cnt[t]
159+
cnt[t]++
160+
}
161+
}
162+
}
163+
}
164+
}
165+
return
166+
}
167+
```
78168

169+
### **TypeScript**
170+
171+
```ts
172+
function countCornerRectangles(grid: number[][]): number {
173+
const n = grid[0].length;
174+
let ans = 0;
175+
const cnt: Map<number, number> = new Map();
176+
for (const row of grid) {
177+
for (let i = 0; i < n; ++i) {
178+
if (row[i] === 1) {
179+
for (let j = i + 1; j < n; ++j) {
180+
if (row[j] === 1) {
181+
const t = i * 200 + j;
182+
ans += cnt.get(t) ?? 0;
183+
cnt.set(t, (cnt.get(t) ?? 0) + 1);
184+
}
185+
}
186+
}
187+
}
188+
}
189+
return ans;
190+
}
79191
```
80192

81193
### **...**

solution/0700-0799/0750.Number Of Corner Rectangles/README_EN.md

+107-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,119 @@
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def countCornerRectangles(self, grid: List[List[int]]) -> int:
56+
ans = 0
57+
cnt = Counter()
58+
n = len(grid[0])
59+
for row in grid:
60+
for i, c1 in enumerate(row):
61+
if c1:
62+
for j in range(i + 1, n):
63+
if row[j]:
64+
ans += cnt[(i, j)]
65+
cnt[(i, j)] += 1
66+
return ans
5567
```
5668

5769
### **Java**
5870

5971
```java
72+
class Solution {
73+
public int countCornerRectangles(int[][] grid) {
74+
int n = grid[0].length;
75+
int ans = 0;
76+
Map<List<Integer>, Integer> cnt = new HashMap<>();
77+
for (var row : grid) {
78+
for (int i = 0; i < n; ++i) {
79+
if (row[i] == 1) {
80+
for (int j = i + 1; j < n; ++j) {
81+
if (row[j] == 1) {
82+
List<Integer> t = List.of(i, j);
83+
ans += cnt.getOrDefault(t, 0);
84+
cnt.merge(t, 1, Integer::sum);
85+
}
86+
}
87+
}
88+
}
89+
}
90+
return ans;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
int countCornerRectangles(vector<vector<int>>& grid) {
101+
int n = grid[0].size();
102+
int ans = 0;
103+
map<pair<int, int>, int> cnt;
104+
for (auto& row : grid) {
105+
for (int i = 0; i < n; ++i) {
106+
if (row[i]) {
107+
for (int j = i + 1; j < n; ++j) {
108+
if (row[j]) {
109+
ans += cnt[{i, j}];
110+
++cnt[{i, j}];
111+
}
112+
}
113+
}
114+
}
115+
}
116+
return ans;
117+
}
118+
};
119+
```
120+
121+
### **Go**
122+
123+
```go
124+
func countCornerRectangles(grid [][]int) (ans int) {
125+
n := len(grid[0])
126+
type pair struct{ x, y int }
127+
cnt := map[pair]int{}
128+
for _, row := range grid {
129+
for i, x := range row {
130+
if x == 1 {
131+
for j := i + 1; j < n; j++ {
132+
if row[j] == 1 {
133+
t := pair{i, j}
134+
ans += cnt[t]
135+
cnt[t]++
136+
}
137+
}
138+
}
139+
}
140+
}
141+
return
142+
}
143+
```
60144

145+
### **TypeScript**
146+
147+
```ts
148+
function countCornerRectangles(grid: number[][]): number {
149+
const n = grid[0].length;
150+
let ans = 0;
151+
const cnt: Map<number, number> = new Map();
152+
for (const row of grid) {
153+
for (let i = 0; i < n; ++i) {
154+
if (row[i] === 1) {
155+
for (let j = i + 1; j < n; ++j) {
156+
if (row[j] === 1) {
157+
const t = i * 200 + j;
158+
ans += cnt.get(t) ?? 0;
159+
cnt.set(t, (cnt.get(t) ?? 0) + 1);
160+
}
161+
}
162+
}
163+
}
164+
}
165+
return ans;
166+
}
61167
```
62168

63169
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int countCornerRectangles(vector<vector<int>>& grid) {
4+
int n = grid[0].size();
5+
int ans = 0;
6+
map<pair<int, int>, int> cnt;
7+
for (auto& row : grid) {
8+
for (int i = 0; i < n; ++i) {
9+
if (row[i]) {
10+
for (int j = i + 1; j < n; ++j) {
11+
if (row[j]) {
12+
ans += cnt[{i, j}];
13+
++cnt[{i, j}];
14+
}
15+
}
16+
}
17+
}
18+
}
19+
return ans;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func countCornerRectangles(grid [][]int) (ans int) {
2+
n := len(grid[0])
3+
type pair struct{ x, y int }
4+
cnt := map[pair]int{}
5+
for _, row := range grid {
6+
for i, x := range row {
7+
if x == 1 {
8+
for j := i + 1; j < n; j++ {
9+
if row[j] == 1 {
10+
t := pair{i, j}
11+
ans += cnt[t]
12+
cnt[t]++
13+
}
14+
}
15+
}
16+
}
17+
}
18+
return
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int countCornerRectangles(int[][] grid) {
3+
int n = grid[0].length;
4+
int ans = 0;
5+
Map<List<Integer>, Integer> cnt = new HashMap<>();
6+
for (var row : grid) {
7+
for (int i = 0; i < n; ++i) {
8+
if (row[i] == 1) {
9+
for (int j = i + 1; j < n; ++j) {
10+
if (row[j] == 1) {
11+
List<Integer> t = List.of(i, j);
12+
ans += cnt.getOrDefault(t, 0);
13+
cnt.merge(t, 1, Integer::sum);
14+
}
15+
}
16+
}
17+
}
18+
}
19+
return ans;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def countCornerRectangles(self, grid: List[List[int]]) -> int:
3+
ans = 0
4+
cnt = Counter()
5+
n = len(grid[0])
6+
for row in grid:
7+
for i, c1 in enumerate(row):
8+
if c1:
9+
for j in range(i + 1, n):
10+
if row[j]:
11+
ans += cnt[(i, j)]
12+
cnt[(i, j)] += 1
13+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function countCornerRectangles(grid: number[][]): number {
2+
const n = grid[0].length;
3+
let ans = 0;
4+
const cnt: Map<number, number> = new Map();
5+
for (const row of grid) {
6+
for (let i = 0; i < n; ++i) {
7+
if (row[i] === 1) {
8+
for (let j = i + 1; j < n; ++j) {
9+
if (row[j] === 1) {
10+
const t = i * 200 + j;
11+
ans += cnt.get(t) ?? 0;
12+
cnt.set(t, (cnt.get(t) ?? 0) + 1);
13+
}
14+
}
15+
}
16+
}
17+
}
18+
return ans;
19+
}

0 commit comments

Comments
 (0)