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. JVM垃圾回收器(三)

    垃圾回收知识点 引用计数 给对象添加一个引用计数器,每当一个地方引用这个对象,这个计算器就加1.如果引用失效,那计算器就减1.如果计算器数量为0,那这个对象就是失效的. 但是如果2个对象虽然不用了,但 ...

  2. 【Scala】关于集合的各种知识点

    目录 映射Map 不可变Map 概述 操作实例 可变Map 概述 操作实例 Map的遍历 for循环遍历 格式 操作实例 模式匹配遍历 格式 操作实例 Tuple 元祖 概述 定义格式 获取元素方法 ...

  3. 基于情感词典的python情感分析

    近期老师给我们安排了一个大作业,要求根据情感词典对微博语料进行情感分析.于是在网上狂找资料,看相关书籍,终于搞出了这个任务.现在做做笔记,总结一下本次的任务,同时也给遇到有同样需求的人,提供一点帮助. ...

  4. Ubuntu系统make menuconfig的依赖包ncurses安装

    Linux内核或者u-boot进行make menuconfig的时候,如果系统上没有安装ncurses,就会出现以下报错 *** Unable to find the ncurses librari ...

  5. Python语法学习第三天--元组

    元组:元组与列表相似,但是元组不能随意修改 ①创建元组 使用圆括号,用逗号隔开 空元组tuple1=()元组中只包含一个元素时,需要在元素后面添加逗号#逗号是关键tuple1=(1,) 当tuple1 ...

  6. [hdu3308]线段树

    题意:单点更新,区间LCIS(最长连续递增序列)查询.具备区间合并维护的性质,不用线段树用什么~ #pragma comment(linker, "/STACK:10240000,10240 ...

  7. Luogu P3389 高斯消元

    https://www.luogu.com.cn/problem/P3389 主元消元法[模板] 高斯消元是解决多元线性方程组的方法,再学习它之前,先引入一个东西--行列式 行列式的性质: 这里我们只 ...

  8. Linux --remote (ssh and VNC)

    远程管理 与个人计算机不同,服务器一般都是运行IDC机房中,所以我们通常不会接触到服务器硬件,而是通过各种远程管理方式对服务器进行控制. 常见的远程管理工具方式: -RDP(remote deskto ...

  9. Azure B2C登录,react-web端实现,自定义登录页面ui

    import React, { Component } from 'react'; import Particles from 'react-particles-js'; import { Form, ...

  10. Colorful String

    Colorful String #include <bits/stdc++.h> using namespace std; typedef long long ll; ; char s[m ...