Java实现 LeetCode 835 图像重叠(暴力)
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 图像重叠(暴力)的更多相关文章
- Java实现 LeetCode 836 矩形重叠(暴力)
836. 矩形重叠 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的 ...
- Java实现 LeetCode 733 图像渲染(DFS)
733. 图像渲染 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间. 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的 ...
- Java实现 LeetCode 435 无重叠区间
435. 无重叠区间 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互"接触& ...
- 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 ...
- Java for LeetCode 044 Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
随机推荐
- Cassandra 简介
Cassandra是云原生和微服务化场景中最好的NoSQL数据库.我信了~ 1. Cassandra是什么 高可用性和可扩展的分布式数据库 Apache Cassandra™是一个开源分布式数据,可提 ...
- Docker知识点整理
目录 1. Docker简介 1.1 Docker是什么 1.2 在隔离的容器中运行软件 1.3 分发容器 2. Docker镜像 2.1 Docker镜像简介 2.2 Docker镜像常见操作 2. ...
- JVM入门--类加载器
一.基础架构 概览 我们平时说的栈是指的Java栈,native method stack 里面装的都是native方法 细节架构图 二.类加载器 1.类的加载 方法区并不是存放方法的区域,其是存放类 ...
- mysql 中 innoDB 与 MySAM
mysql 中 innoDB 与 MyISAM 的特点 --ENGINE = innodb 1.提供事务处理,支持行锁: 2.不加锁读取,增加并发读的用户数量和空间: 3. insert/update ...
- python --error整理(不定时更新)
1.TabError: inconsistent use of tabs and spaces in indentation Python 中需要用tab 键来空格 2.SyntaxError: in ...
- 一个学习 Koa 源码的例子
作者: MarkLin 学习目标: 原生 node 封装 中间件 路由 Koa 原理 一个 nodejs 的入门级 http 服务代码如下, // index.js const http = requ ...
- python之感知器-从零开始学深度学习
感知器-从零开始学深度学习 未来将是人工智能和大数据的时代,是各行各业使用人工智能在云上处理大数据的时代,深度学习将是新时代的一大利器,在此我将从零开始记录深度学习的学习历程. 我希望在学习过程中做到 ...
- 2.2 Go变量类型
内置类型 值类型: bool 布尔类型 int(32 or 64), int8, int16, int32, int64 整数类型 uint(32 or 64), uint8(byte), uint1 ...
- 201771010117马兴德 实验二 Java基本程序设计(1)
实验二 Java基本程序设计(1) 第一部分 理论知识的学习. 第三章Java基本程序设计结构 1 基本知识: (1)标识符:标识符由字母.下划线.美元符号和数字组成,且第一个符号不能为数字 ...
- vue2.0 axios前后端数据处理
目前主流的 Vue 项目,都选择 axios 来完成 ajax 请求,而大型项目都会使用 Vuex 来管理数据. 前言: 使用 cnpm 安装 axios cnpm install axios -S ...