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 ...
随机推荐
- 07_CSS入门和高级技巧(5)
超级链接美化 1.伪类 同一个超级链接,根据用户的点击情况,有自己样式: 超级链接根据用户点选情况,有4种状态: a:link 没有访问的超级链接 a:visited 已经访问的超级链接 a:hove ...
- Qt之分模块log
说明 对于一般的log,使用 qInstallMessageHandler 重定向到文件即可,甚至可以根据日志等级,分类存储.但是并不是适用所有情况,比如,程序运行时动态创建模块,而每个模块需要创建不 ...
- python重试次数装饰器
目录 重试次数装饰器 重试次数装饰器 前言, 最近在使用tornado框架写Restful API时遇到很多的问题. 有框架的问题, 有异步的问题. 虽然tornado 被公认为当前python语言最 ...
- 线程和Python—Python多线程编程
线程和Python 本节主要记录如何在 Python 中使用线程,其中包括全局解释器锁对线程的限制和对应的学习脚本. 全局解释器锁 Python 代码的执行是由 Python 虚拟机(又叫解释器主循环 ...
- Codeforces 1272E (Nearest Opposite Parity,反向建边)
题意:给你n个数,每个数的值为a[i],每个点可以从i这号点跳转至(i - a[i]) 或 (i + a[i])点,点的范围为[1,n],然后问的是从偶数点跳至奇数点,从奇数点跳至偶数点的最少次数是多 ...
- Arthas 使用(一) —— 基础命令
Arthas 简介 Arthas 是 Alibaba 开源的 Java 诊断工具,根据官方介绍,它提供了如下工功能: 官方文档地址: https://alibaba.github.io/arthas/ ...
- node的url模块
.parse(url,query2obj[boolean],ignorePrototype[boolean]) .format({}) 和.parse相反,将带有url参数属性的对象组装成url .r ...
- MySQL用户、库、表(单/多)操作
用户及权限操作: 管理员登录:mysql -uroot -p 用户设置密码:set password=password(密码); 查看数据库所有用户:select * from mysql.user; ...
- 13.1 Go练习题
13.1 Go练习题 创建一个goroutine与主线程按顺序相互发送信息若干次 且打印 slice在自动扩容后,内存地址变化 goroutine与闭包的坑 练习题汇总 package main fu ...
- 2.6 Golang命令
在命令行执行go命令查看相关的Go语言命令: Go is a tool for managing Go source code. Usage: go command [arguments] The c ...