【LeetCode】750. Number Of Corner Rectangles 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/number-of-corner-rectangles/
题目描述
Given a grid where each entry is only 0 or 1, find the number of corner rectangles.
A corner rectangle is 4 distinct 1s on the grid that form an axis-aligned rectangle. Note that only the corners need to have the value 1. Also, all four 1s used must be distinct.
Example 1:
Input: grid =
[[1, 0, 0, 1, 0],
[0, 0, 1, 0, 1],
[0, 0, 0, 1, 0],
[1, 0, 1, 0, 1]]
Output: 1
Explanation: There is only one corner rectangle, with corners grid[1][2], grid[1][4], grid[3][2], grid[3][4].
Example 2:
Input: grid =
[[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]
Output: 9
Explanation: There are four 2x2 rectangles, four 2x3 and 3x2 rectangles, and one 3x3 rectangle.
Example 3:
Input: grid =
[[1, 1, 1, 1]]
Output: 0
Explanation: Rectangles must have four distinct corners.
Note:
- The number of rows and columns of grid will each be in the range
[1, 200]. - Each
grid[i][j]will be either 0 or 1. - The number of 1s in the grid will be at most 6000.
题目大意
给定一个只包含 0 和 1 的网格,找出其中角矩形的数量。
一个 角矩形 是由四个不同的在网格上的 1 形成的轴对称的矩形。注意只有角的位置才需要为 1。并且,4 个 1 需要是不同的。
解题方法
遍历
一定需要四重循环分别维护矩形的上下左右四条边吗?答案是否定的。
可以固定矩形的上下两条边界,然后遍历其中的每一列,统计这两行一列的交点是否都为1,所有都为1的列累计得到count。那么在固定此上下两条边界的情况下,能组成的所有角都为1的矩形是从count条边中选择两条不同的,答案是count * (count - 1) / 2.
总共是三重循环,时间复杂度是O(M^2*N).
C++代码如下:
class Solution {
public:
int countCornerRectangles(vector<vector<int>>& grid) {
if (!grid.size() || !grid[0].size()) return 0;
const int M = grid.size();
const int N = grid[0].size();
int res = 0;
for (int row1 = 0; row1 < M; ++row1) {
for (int row2 = row1 + 1; row2 < M; ++row2) {
int count = 0;
for (int col = 0; col < N; ++col) {
if (grid[row1][col] == 1 && grid[row2][col] == 1) {
count ++;
}
}
res += count * (count - 1) / 2;
}
}
return res;
}
};
参考资料:https://leetcode-cn.com/problems/number-of-corner-rectangles/solution/java-by-zxy0917-16/
日期
2019 年 9 月 24 日 —— 梦见回到了小学,小学已经芳草萋萋破败不堪
【LeetCode】750. Number Of Corner Rectangles 解题报告 (C++)的更多相关文章
- [LeetCode] 750. Number Of Corner Rectangles 边角矩形的数量
Given a grid where each entry is only 0 or 1, find the number of corner rectangles. A corner rectang ...
- leetcode 750. Number Of Corner Rectangles
Given a grid where each entry is only 0 or 1, find the number of corner rectangles. A corner rectang ...
- 750. Number Of Corner Rectangles四周是点的矩形个数
[抄题]: Given a grid where each entry is only 0 or 1, find the number of corner rectangles. A corner r ...
- [LeetCode] Number Of Corner Rectangles 边角矩形的数量
Given a grid where each entry is only 0 or 1, find the number of corner rectangles. A corner rectang ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
随机推荐
- [R] ignore.case区分大小写参数
字符串操作的函数(如contains),很多都包含ignore.case参数,默认是T,即不分大小写,稍不注意就会掉坑里,最好的习惯是下意识地加入这个参数. 举个例子: 我要选择An的列,就用下面这个 ...
- Java的那些小事
一,JDK和JRE有什么区别? JRE:Java Runtime Environment(java运行时环境).即java程序的运行时环境,包含了java虚拟机,java基础类库. JDK:Java ...
- 日常Java 2021/10/25
ArrayList存储数字 import java.util.ArrayList; public class Arr_test { public static void main(String[] a ...
- 学习java 7.28
学习内容: Applet Applet一般称为小应用程序,Java Applet就是用Java语言编写的这样的一些小应用程序,它们可以通过嵌入到Web页面或者其他特定的容器中来运行,也可以通过Java ...
- Flume(一)【概述】
目录 一.Flume定义 二.Flume基础架构 1.Agent 2.Source 3.Sink 4.Channel 5.Event 一.Flume定义 Flume是Cloudera公司提供的一个 ...
- Hive(四)【DML 数据导入导出】
目录 一.数据导入 1.1 [load]--向数据中装载数据 案例 1.2 [insert]--查询语句向表中插入数据 案例 1.3 [as select]--查询语句中创建表且加载数据 案例 1.4 ...
- 大数据学习day13------第三阶段----scala01-----函数式编程。scala以及IDEA的安装,变量的定义,条件表达式,for循环(守卫模式,推导式,可变参数以及三种遍历方式),方法定义,数组以及集合(可变和非可变),数组中常用的方法
具体见第三阶段scala-day01中的文档(scala编程基础---基础语法) 1. 函数式编程(https://www.cnblogs.com/wchukai/p/5651185.html): ...
- 寻找pair
给定n个整数使其两两组合成一对pair,例如给定 1 ,2 可以组成的pair为(1,1),(1,2),(2,1),(2,2),然后在这些pair中寻找第k小的pair. 输入第一行包含两个数字,第一 ...
- AI ubantu 环境安装
ubantu安装记录 apt install python3-pip anaconda安装 https://repo.anaconda.com/archive/Anaconda3-2020.11-Li ...
- 利用Lombok编写优雅的spring依赖注入代码,去掉繁人的@Autowired
大家平时使用spring依赖注入,都是怎么写的? @Servicepublic class OrderService {@Autowiredprivate UserService userServic ...