【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/ 题 ...
随机推荐
- Yii2 源码分析 入口文件执行流程
Yii2 源码分析 入口文件执行流程 1. 入口文件:web/index.php,第12行.(new yii\web\Application($config)->run()) 入口文件主要做4 ...
- ZAQI
mysql> CREATE TABLE emploee ( -> name CHAR(64) NOT NULL, -> email CHAR(64), -> password ...
- MariaDB——简介
一.MariaDB跟MySQL在绝大多数方面是兼容的,对于开发者来说,几乎感觉不到任何不同.是MySQL的代替品. MariaDB虽然被视为MySQL数据库的替代品,但它在扩展功能.存储引擎以及一些新 ...
- 准确率,召回率,F值,ROC,AUC
度量表 1.准确率 (presion) p=TPTP+FP 理解为你预测对的正例数占你预测正例总量的比率,假设实际有90个正例,10个负例,你预测80(75+,5-)个正例,20(15+,5-)个负例 ...
- 生成随机数的N种方式
首先需要说明的是,计算机中生成的随机数严格来说都是伪随机,即非真正的随机数,真正随机数的随机样本不可重现.那么我们来看看代码中有哪些方式可以生成随机数. rand rand函数声明如下: #inclu ...
- Visual Studio Code常用操作整理
Live Server插件可以在保存html文件后实时地刷新页面 在html文件中键入"! +Tap"会生成一个html模板 保存文件:Ctrl+S 文件跳转:Ctrl+P 文件内 ...
- 06 windows安装Python+Pycharm+Scrapy环境
windows安装Python+Pycharm+Scrapy环境 使用微信扫码关注微信公众号,并回复:"Python工具包",免费获取下载链接! 一.卸载python环境 卸载以下 ...
- springcloud - alibaba - 2 - 集成Feign - 更新完成
1.依赖 依赖管理 <parent> <artifactId>spring-boot-parent</artifactId> <groupId>org. ...
- 非寻常方式学习ApacheTomcat架构及10.0.12源码编译
概述 开启博客分享已近三个月,感谢所有花时间精力和小编一路学习和成长的伙伴们,有你们的支持,我们继续再接再厉 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 Tomcat官 ...
- Spark Stage 的划分
Spark作业调度 对RDD的操作分为transformation和action两类,真正的作业提交运行发生在action之后,调用action之后会将对原始输入数据的所有transformation ...