链接:https://leetcode-cn.com/problems/image-overlap/
这道题需要用枚举法对偏移量计数,枚举A中每个1在经过何种滑动之后可以与B中的1重叠。注意这道题中的1滑动过界不会在另一端出现的,相当于是补0的移位操作。
java代码:
class Solution {
public int largestOverlap(int[][] A, int[][] B) {
int count[][] = new int [2*A.length][2*A.length];
for(int i = 0;i<A.length;i++)
for(int j = 0;j<A.length;j++)
if(A[i][j]==1)
for(int m = 0;m<B.length;m++)
for(int n = 0;n<B.length;n++)
if(B[m][n]==1)
count[m-i+A.length][n-j+A.length]++;
int max = 0;
for(int i = 0;i<count.length;i++)
for(int j = 0;j<count.length;j++)
if(count[i][j]>max)
max = count[i][j];
return max;
}
}