Leetcode:835. 图像重叠

直接找出所有1的位置,然后对两个矩阵的所有这些位置进行求差。然后统计这些差出现最多的次数是多少。

两个坐标的差是什么含义?就是把其中一个坐标移动到另一个坐标需要移动的向量。因此,在遍历过程中,我们找出了A中所有值为1的坐标移动到B中所有值为1的坐标需要移动的向量。那么,在这些向量中出现次数最多的向量就是我们要求的整个矩阵应该移动的向量。这个向量出现的次数,就是我们向该向量方向移动了之后,能重叠的1的个数。

寒神的做法的优点:第一,注意到了题目给的A和B是大小相等的正方形!第二,遍历正方形的方式使用的是i在[0,NN]区间里,然后 [i/N][i%N] 这个求位置方法,可以把两重循环简写成一重(但是时间复杂没有变化)。第三,使用了数字表示向量,即把一个向量的行数100+列数,比如第13行第19列,可以用一个数字表示1319。寒神告诉我们,这个100的选择是因为太小的话不能有效区分,应该最小是2N。

public int largestOverlap(int[][] A, int[][] B) {
    int N = A.length;
    List<Integer> LA = new ArrayList<>();
    List<Integer> LB = new ArrayList<>();
    HashMap<Integer, Integer> count = new HashMap<>();
    for (int i = 0; i < N * N; ++i) if (A[i / N][i % N] == 1) LA.add(i / N * 100 + i % N);
    for (int i = 0; i < N * N; ++i) if (B[i / N][i % N] == 1) LB.add(i / N * 100 + i % N);
    for (int i : LA) for (int j : LB)
            count.put(i - j, count.getOrDefault(i - j, 0) + 1);
    int res = 0;
    for (int i : count.values()) res = Math.max(res, i);
    return res;
}

原文地址:https://blog.51cto.com/f1yinsky/2376421

时间: 2024-07-30 17:10:20

Leetcode:835. 图像重叠的相关文章

[LeetCode] Image Overlap 图像重叠

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 o

Leetcode 835. Image Overlap.md

题目 链接: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 cho

[Swift]LeetCode835. 图像重叠 | Image Overlap

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 o

LeetCode 29. 矩形重叠 反向思维

题目描述 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的是,只在角或边接触的两个矩形不构成重叠. 给出两个矩形,判断它们是否重叠并返回结果. 示例 1: 输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3] 输出:true 示例 2: 输入:rec1 = [0,0,1,1], rec2 = [1,0,2,1] 输出:false 提示: 两个矩

Leetcode 836. 矩形重叠

矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的是,只在角或边接触的两个矩形不构成重叠. 给出两个矩形,判断它们是否重叠并返回结果. 示例 1: 输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3] 输出:true 示例 2: 输入:rec1 = [0,0,1,1], rec2 = [1,0,2,1] 输出:false 提示: 两个矩形 rec

leet

# 题名1 两数之和    2 两数相加    3 无重复字符的最长子串    4 寻找两个有序数组的中位数    5 最长回文子串    6 Z 字形变换    7 整数反转    8 字符串转换整数 (atoi)    9 回文数    10 正则表达式匹配    11 盛最多水的容器    12 整数转罗马数字    13 罗马数字转整数    14 最长公共前缀    15 三数之和    16 最接近的三数之和    17 电话号码的字母组合    18 四数之和    19 删除链表

基于轮廓的匹配算法(强,可在重叠堆积物体中识别)

这是一篇印度软件工程师的无私奉献! 非常具备参考价值! 如果能完成旋转匹配更接近于实用性. 当然要完成全角度匹配的难度是要量级数的提升. Download source - 140 KB Download demo - 138 KB Introduction Template matching is an image processing problem to find the location of an object using a template image in another sea

Android Canvas设置绘画时重叠部分的处理模式【含效果图】

在Android的PorterDuff.Mode类中列举了他们制定的规则: android.graphics.PorterDuff.Mode.SRC:只绘制源图像 android.graphics.PorterDuff.Mode.DST:只绘制目标图像 android.graphics.PorterDuff.Mode.DST_OVER:在源图像的顶部绘制目标图像 android.graphics.PorterDuff.Mode.DST_IN:只在源图像和目标图像相交的地方绘制目标图像 andro

leetcode 做过的题目总结

双指针: leetcode167. 两数之和 II - 输入有序数组(双指针) leetcode 633.平方数之和(双指针 java) leetcode 345.反转字符串中的元音字母(双指针 java)(有地方不懂) leetcode 680.验证回文字符串 II(双指针 java) leetcode 88.合并两个有序数组(双指针 java) leetcode 141.环形链表(双指针 java) 排序: leetcode215.数组中的第k个最大元素(快排/堆排序 java)*** 只写