|
61 | 61 |
|
62 | 62 | <!-- 这里可写通用的实现逻辑 -->
|
63 | 63 |
|
| 64 | +**方法一:枚举** |
| 65 | + |
| 66 | +我们可以枚举 $img1$ 和 $img2$ 的每个 $1$ 的位置,分别记为 $(i, j)$ 和 $(h, k)$。然后我们计算得到偏移量 $(i - h, j - k)$,记为 $(dx, dy)$,用哈希表 $cnt$ 记录每个偏移量出现的次数。最后我们遍历哈希表 $cnt$,找到出现次数最多的偏移量,即为答案。 |
| 67 | + |
| 68 | +时间复杂度 $O(n^4)$,空间复杂度 $O(n^2)$。其中 $n$ 是 $img1$ 的边长。 |
| 69 | + |
64 | 70 | <!-- tabs:start -->
|
65 | 71 |
|
66 | 72 | ### **Python3**
|
67 | 73 |
|
68 | 74 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
69 | 75 |
|
70 | 76 | ```python
|
71 |
| - |
| 77 | +class Solution: |
| 78 | + def largestOverlap(self, img1: List[List[int]], img2: List[List[int]]) -> int: |
| 79 | + n = len(img1) |
| 80 | + cnt = Counter() |
| 81 | + for i in range(n): |
| 82 | + for j in range(n): |
| 83 | + if img1[i][j]: |
| 84 | + for h in range(n): |
| 85 | + for k in range(n): |
| 86 | + if img2[h][k]: |
| 87 | + cnt[(i - h, j - k)] += 1 |
| 88 | + return max(cnt.values()) if cnt else 0 |
72 | 89 | ```
|
73 | 90 |
|
74 | 91 | ### **Java**
|
75 | 92 |
|
76 | 93 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
77 | 94 |
|
78 | 95 | ```java
|
| 96 | +class Solution { |
| 97 | + public int largestOverlap(int[][] img1, int[][] img2) { |
| 98 | + int n = img1.length; |
| 99 | + Map<List<Integer>, Integer> cnt = new HashMap<>(); |
| 100 | + int ans = 0; |
| 101 | + for (int i = 0; i < n; ++i) { |
| 102 | + for (int j = 0; j < n; ++j) { |
| 103 | + if (img1[i][j] == 1) { |
| 104 | + for (int h = 0; h < n; ++h) { |
| 105 | + for (int k = 0; k < n; ++k) { |
| 106 | + if (img2[h][k] == 1) { |
| 107 | + List<Integer> t = List.of(i - h, j - k); |
| 108 | + ans = Math.max(ans, cnt.merge(t, 1, Integer::sum)); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + return ans; |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +### **C++** |
| 121 | + |
| 122 | +```cpp |
| 123 | +class Solution { |
| 124 | +public: |
| 125 | + int largestOverlap(vector<vector<int>>& img1, vector<vector<int>>& img2) { |
| 126 | + int n = img1.size(); |
| 127 | + map<pair<int, int>, int> cnt; |
| 128 | + int ans = 0; |
| 129 | + for (int i = 0; i < n; ++i) { |
| 130 | + for (int j = 0; j < n; ++j) { |
| 131 | + if (img1[i][j]) { |
| 132 | + for (int h = 0; h < n; ++h) { |
| 133 | + for (int k = 0; k < n; ++k) { |
| 134 | + if (img2[h][k]) { |
| 135 | + ans = max(ans, ++cnt[{i - h, j - k}]); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + return ans; |
| 143 | + } |
| 144 | +}; |
| 145 | +``` |
| 146 | +
|
| 147 | +### **Go** |
| 148 | +
|
| 149 | +```go |
| 150 | +func largestOverlap(img1 [][]int, img2 [][]int) (ans int) { |
| 151 | + type pair struct{ x, y int } |
| 152 | + cnt := map[pair]int{} |
| 153 | + for i, row1 := range img1 { |
| 154 | + for j, x1 := range row1 { |
| 155 | + if x1 == 1 { |
| 156 | + for h, row2 := range img2 { |
| 157 | + for k, x2 := range row2 { |
| 158 | + if x2 == 1 { |
| 159 | + t := pair{i - h, j - k} |
| 160 | + cnt[t]++ |
| 161 | + ans = max(ans, cnt[t]) |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + return |
| 169 | +} |
| 170 | +
|
| 171 | +func max(a, b int) int { |
| 172 | + if a > b { |
| 173 | + return a |
| 174 | + } |
| 175 | + return b |
| 176 | +} |
| 177 | +``` |
79 | 178 |
|
| 179 | +### **TypeScript** |
| 180 | + |
| 181 | +```ts |
| 182 | +function largestOverlap(img1: number[][], img2: number[][]): number { |
| 183 | + const n = img1.length; |
| 184 | + const cnt: Map<number, number> = new Map(); |
| 185 | + let ans = 0; |
| 186 | + for (let i = 0; i < n; ++i) { |
| 187 | + for (let j = 0; j < n; ++j) { |
| 188 | + if (img1[i][j] === 1) { |
| 189 | + for (let h = 0; h < n; ++h) { |
| 190 | + for (let k = 0; k < n; ++k) { |
| 191 | + if (img2[h][k] === 1) { |
| 192 | + const t = (i - h) * 200 + (j - k); |
| 193 | + cnt.set(t, (cnt.get(t) ?? 0) + 1); |
| 194 | + ans = Math.max(ans, cnt.get(t)!); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + return ans; |
| 202 | +} |
80 | 203 | ```
|
81 | 204 |
|
82 | 205 | ### **...**
|
|
0 commit comments