835. 图像重叠

给出两个图像 A 和 B ,A 和 B 为大小相同的二维正方形矩阵。(并且为二进制矩阵,只包含0和1)。

我们转换其中一个图像,向左,右,上,或下滑动任何数量的单位,并把它放在另一个图像的上面。之后,该转换的重叠是指两个图像都具有 1 的位置的数目。

(请注意,转换不包括向任何方向旋转。)

最大可能的重叠是什么?

示例 1:

输入:A = [[1,1,0],

[0,1,0],

[0,1,0]]

B = [[0,0,0],

[0,1,1],

[0,0,1]]

输出:3

解释: 将 A 向右移动一个单位,然后向下移动一个单位。

注意:

1 <= A.length = A[0].length = B.length = B[0].length <= 30

0 <= A[i][j], B[i][j] <= 1

class Solution {
public int largestOverlap(int[][] A, int[][] B) {
int max = 0;
int length = A.length; for(int i = 0; i < length; i++) {
for(int j = 0; j < length; j++) {
max = Math.max(max, countOverlap(A, B, i, j));
max = Math.max(max, countOverlap(B, A, i, j));
}
} return max;
} private int countOverlap(int[][] A, int[][] B, int m, int n) {
int length = A.length;
int count = 0; for(int i = m; i < length; i++) {
for(int j = n; j < length; j++) {
count += A[i][j] & B[i-m][j-n];
}
} return count;
} }

Java实现 LeetCode 835 图像重叠(暴力)的更多相关文章

  1. Java实现 LeetCode 836 矩形重叠(暴力)

    836. 矩形重叠 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的 ...

  2. Java实现 LeetCode 733 图像渲染(DFS)

    733. 图像渲染 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间. 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的 ...

  3. Java实现 LeetCode 435 无重叠区间

    435. 无重叠区间 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互"接触& ...

  4. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. Java for LeetCode 044 Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  6. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  7. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  8. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  9. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

随机推荐

  1. javaScript 添加和移除class类名的几种方法

    添加类属性: // 一次只能设置一个类值,如果当前属性本身存在类值,会被替换 element.className = '类名'; /* * .setAttribute 用来设置自定义属性和值的 * 自 ...

  2. docker环境中neo4j导入导出

    neo4j 官方文档有说明,使用 neo4j-admin restore / dump 导出和恢复数据库的时候需要停掉数据,否则会报数据库正在使用的错误:command failed: the dat ...

  3. XSS挑战之旅(通过看代码解题)

    XSS 挑战之旅 level 1 没有什么过滤 payload: <script>alert(1)</script> level 2 php关键代码: echo "& ...

  4. tomcat 添加 ssl 证书

    1. 将证书提供方给的证书(server.crt)及密钥文件(server.key)上传到服务器 tomcat 的 conf 目录 2. 在tomcat conf 目录下执行如下命令 (1) 生成P1 ...

  5. 【python(deap库)实现】GEAP 遗传算法/遗传编程 genetic programming +

    目录 前言 1.优化问题的定义 单目标优化 多目标优化 2.个体编码 实数编码 二进制编码 序列编码(Permutation encoding) 粒子(Particles) 3 初始种群建立 一般族群 ...

  6. County Fair Events

    先按照结束时间进行排序,取第一个节日的结束时间作为当前时间,然后从第二个节日开始搜索,如果下一个节日的开始时间大于当前的时间,那么就参加这个节日,并更新当前时间 #include <bits/s ...

  7. 利用js实现 禁用浏览器后退| 去除上一个历史记录链接

    也是查找了好多资料才找到的,这种方式,可以消除 后退的所有动作.包括 键盘.鼠标手势等产生的后退动作. <script language="javascript"> / ...

  8. uefi win10 Ubuntu 18的安装

    uefi win10 Ubuntu 18的安装 (Ubuntu折腾的第一天) 安装时的踩坑记录

  9. PAT-1080 Graduate Admission (结构体排序)

    1080. Graduate Admission It is said that in 2013, there were about 100 graduate schools ready to pro ...

  10. PHP实现白名单或黑名单

    /**  * 安全IP检测,支持IP段检测  * @param string $ip 要检测的IP  * @param string|array $ips  白名单IP或者黑名单IP  * @retu ...