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. Python网络爬虫练习

    1. 豆瓣top250电影 1.1 查看网页 目标网址:https://movie.douban.com/top250?start=0&filter= start=后面的数字从0,25,50一 ...

  2. pyhanlp安装成功,import导入失败,出现:importerror: cannot import name 'jvmnotfoundexception'

    1.问题描述: pyhanlp成功安装,并且可以正常使用,但是这段时间再去用的时候,发现出问题了,一运行就出现,下面的问题: importerror: cannot import name 'jvmn ...

  3. 解决anaconda与pycharm冲突导致import无法使用

    解决annacode与python冲突: 一.File->Setting-> 点击Add-> 然后就完美解决 二.记得重启,检查创建的项目是右键python package-> ...

  4. angular js 分页

    一.编写实体类PageResult public class PageResult implements Serializable { private Long total;//总记录数 privat ...

  5. 【漫画】读写锁ReadWriteLock还是不够快?再试试StampedLock!

    本文来源于公众号[胖滚猪学编程] 转载请注明出处! 在互斥锁ReentrantLock不好用?试试读写锁ReadWriteLock一文中,我们对比了互斥锁ReentrantLock和读写锁ReadWr ...

  6. 神奇的'license': 'AGPL 3.0'标签报错

    在__minifest__.py中,放了license标签,然后整个模块就报错了. 注释掉这个标签就好了.

  7. vue 自己写组件。

    最近在网上看到很多大神都有写博客的习惯,坚持写博客不但可以为自己的平时的学习做好记录积累 无意之中也学还能帮助到一些其他的朋友所以今天我也注册一个账号记录一下学习的点滴!当然本人能力实在有限写出的文章 ...

  8. linux下获取软件源码包 centos/redhat, debian/ubuntu

    linux下获取软件源码包 centos/redhat, debian/ubuntu centos下: 1. yum install yum-utils 主要为了获取yumdownloader 2. ...

  9. Apache自定义404

    先用命令找到httpd.conf文件在哪 find -name 'httpd.conf' 默认配置文件: vim /etc/httpd/conf/httpd.conf 然后找到项目的路径 <Di ...

  10. 使用脚手架 vue-cli 4.0以上版本创建vue项目

    1. 什么是 Vue CLI 如果你只是简单写几个Vue的Demo程序, 那么你不需要Vue CLI:如果你在开发大型项目, 那么你需要, 并且必然需要使用Vue CLI. 使用Vue.js开发大型应 ...