|
60 | 60 |
|
61 | 61 | <!-- 这里可写通用的实现逻辑 -->
|
62 | 62 |
|
| 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 | + |
63 | 69 | <!-- tabs:start -->
|
64 | 70 |
|
65 | 71 | ### **Python3**
|
66 | 72 |
|
67 | 73 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
68 | 74 |
|
69 | 75 | ```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 |
71 | 89 | ```
|
72 | 90 |
|
73 | 91 | ### **Java**
|
74 | 92 |
|
75 | 93 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
76 | 94 |
|
77 | 95 | ```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 | +``` |
78 | 168 |
|
| 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 | +} |
79 | 191 | ```
|
80 | 192 |
|
81 | 193 | ### **...**
|
|
0 commit comments