标签:not tput 直接 down uri possible 没有想到 运行时间 continue
链接:https://leetcode.com/problems/image-overlap/
Level: Medium
Discription:
Two images A and B are given, represented as binary, square matrices of the same size. (A binary matrix has only 0s and 1s as values.)
We translate one image however we choose (sliding it left, right, up, or down any number of units), and place it on top of the other image. After, the overlap of this translation is the number of positions that have a 1 in both images.
(Note also that a translation does not include any kind of rotation.)
What is the largest possible overlap?
Example 1:
Input: A = [[1,1,0],
[0,1,0],
[0,1,0]]
B = [[0,0,0],
[0,1,1],
[0,0,1]]
Output: 3
Explanation: We slide A to right by 1 unit and down by 1 unit.
Note:
class Solution {
public:
int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
map<vector<int>,int> count;
int max=0;
for(int i=0; i<A[0].size()*A[0].size(); i++ )
{
int AX=i/A[0].size();
int AY=i%A[0].size();
if(A[AX][AY]==0)
continue;
for(int j=0;j<B[0].size()*B[0].size(); j++)
{
int BX=j/B[0].size();
int BY=j%B[0].size();
if(B[BX][BY]==1)
{
vector<int> temp;
temp.push_back(BX-AX);
temp.push_back(BY-AY);
if(count.find(temp)!=count.end())
{
count[temp]++;
}
else
count[temp]=1;
max = count[temp]>max ? count[temp] : max;
}
}
}
return max;
}
};
Leetcode 835. Image Overlap.md
标签:not tput 直接 down uri possible 没有想到 运行时间 continue
原文地址:https://www.cnblogs.com/zuotongbin/p/10498673.html