-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path750.cpp
37 lines (37 loc) · 1.35 KB
/
750.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
37
__________________________________________________________________________________________________
class Solution {
public:
int countCornerRectangles(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size(), res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 0) continue;
for (int h = 1; h < m - i; ++h) {
if (grid[i + h][j] == 0) continue;
for (int w = 1; w < n - j; ++w) {
if (grid[i][j + w] == 1 && grid[i + h][j + w] == 1) ++res;
}
}
}
}
return res;
}
};
__________________________________________________________________________________________________
class Solution {
public:
int countCornerRectangles(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size(), res = 0;
for (int i = 0; i < m; ++i) {
for (int j = i + 1; j < m; ++j) {
int cnt = 0;
for (int k = 0; k < n; ++k) {
if (grid[i][k] == 1 && grid[j][k] == 1) ++cnt;
}
res += cnt * (cnt - 1) / 2;
}
}
return res;
}
};
__________________________________________________________________________________________________