[LeetCode] 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 either0
or1
. - The number of
1
s in the grid will be at most6000
.
这道题给了我们一个由0和1组成的二维数组,这里定义了一种边角矩形,其四个顶点均为1,让我们求这个二维数组中有多少个不同的边角矩形。那么最简单直接的方法就是暴力破解啦,我们遍历所有的子矩形,并且检验其四个顶点是否为1即可。先确定左上顶点,每个顶点都可以当作左上顶点,所以需要两个for循环,然后我们直接跳过非1的左上顶点,接下来就是要确定右上顶点和左下顶点了,先用一个for循环确定左下顶点的位置,同理,如果左下顶点为0,直接跳过。再用一个for循环确定右上顶点的位置,如果右上顶点位置也确定了,那么此时四个顶点中确定了三个,右下顶点的位置也就确定了,此时如果右上和右下顶点均为1,则结果res自增1,参见代码如下:
解法一:
- class Solution {
- public:
- int countCornerRectangles(vector<vector<int>>& grid) {
- int m = grid.size(), n = grid[].size(), res = ;
- for (int i = ; i < m; ++i) {
- for (int j = ; j < n; ++j) {
- if (grid[i][j] == ) continue;
- for (int h = ; h < m - i; ++h) {
- if (grid[i + h][j] == ) continue;
- for (int w = ; w < n - j; ++w) {
- if (grid[i][j + w] == && grid[i + h][j + w] == ) ++res;
- }
- }
- }
- }
- return res;
- }
- };
我们来看一种优化了时间复杂度的方法,这种方法的原理是两行同时遍历,如果两行中相同列位置的值都为1,则计数器cnt自增1,那么最后就相当于有了(cnt - 1)个相邻的格子,问题就转化为了求cnt-1个相邻的格子能组成多少个矩形,就变成了初中数学问题了,共有cnt*(cnt-1)/2个,参见代码如下:
解法二:
- class Solution {
- public:
- int countCornerRectangles(vector<vector<int>>& grid) {
- int m = grid.size(), n = grid[].size(), res = ;
- for (int i = ; i < m; ++i) {
- for (int j = i + ; j < m; ++j) {
- int cnt = ;
- for (int k = ; k < n; ++k) {
- if (grid[i][k] == && grid[j][k] == ) ++cnt;
- }
- res += cnt * (cnt - ) / ;
- }
- }
- return res;
- }
- };
下面这种解法由热心网友edyyy提供,最大亮点是将解法二的beat 65%提高到了beat 97%,速度杠杠的,要飞起来了的节奏。在遍历前一行的时候,将所有为1的位置存入到了一个数组ones中,然后在遍历其他行时,直接检测ones数组中的那些位置是否为1,这样省去了检查一些之前行为0的步骤,提高了运行速度,但是也牺牲了一些空间,比如需要ones数组,算是个trade off吧,参见代码如下:
解法三:
- class Solution {
- public:
- int countCornerRectangles(vector<vector<int>>& grid) {
- int m = grid.size(), n = grid[].size(), res = ;
- for (int i = ; i < m - ; i++) {
- vector<int> ones;
- for (int k = ; k < n; k++) if (grid[i][k]) ones.push_back(k);
- for (int j = i + ; j < m; j++) {
- int cnt = ;
- for (int l = ; l < ones.size(); l++) {
- if (grid[j][ones[l]]) cnt++;
- }
- res += cnt * (cnt - ) / ;
- }
- }
- return res;
- }
- };
参考资料:
https://discuss.leetcode.com/topic/114177/short-java-ac-solution-o-m-2-n-with-explanation
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Number Of Corner Rectangles 边角矩形的数量的更多相关文章
- [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】750. Number Of Corner Rectangles 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 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 ...
- 2016.5.15——leetcode:Number of 1 Bits ,
leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...
- LeetCode——Number Complement
LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...
- LeetCode——Number of Boomerangs
LeetCode--Number of Boomerangs Question Given n points in the plane that are all pairwise distinct, ...
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Number of Boomerangs 回旋镖的数量
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
随机推荐
- xml解析多个结点方法(C#)
解析多个结点的XML文件,格式如下: <?xml version="1.0" encoding="utf-8"?> <response> ...
- linux下文件的复制、移动与删除命令为:cp,mv,rm
一.文件复制命令cp 命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination) cp [option] source1 source2 sour ...
- hibernate框架学习笔记12:查询优化
类级别查询优化: 创建一个实体类: package domain; import java.util.HashSet; import java.util.Set; //客户实体 public clas ...
- C 连接mysql VC的步骤
初学C,看到C 连接mysql的教程不是很多,遇到很多的问题,看过许多盟友的解决方法,有点模糊(对我这个菜鸟来说),下面贴出具体步骤,一起学习: 1.C连接mysql的方法:C ,C ++ ,ODBC ...
- eclipse配置svn方法
一.在Eclipse里下载Subclipse插件 方法一:从Eclipse Marketplace里面下载 具体操作:打开Eclipse --> Help --> Eclipse Mark ...
- %f使用时的注意事项
1不是所有定义都用int,使用浮点函数需要把int改成float才能正常工作 2保留一位小数时要打入%0.1f,保留两位小数时要打入%0.2f,而不是%0.01f
- 学号:201621123032 《Java程序设计》第8周学习总结
1:本周学习总结 2:书面作业 2.1:ArrayList代码分析 2.1.1:解释ArrayList的contains源代码 Contains方法调用indexof方法,如果元素为null,则循环比 ...
- day-7 一个简单的决策树归纳算法(ID3)python编程实现
本文介绍如何利用决策树/判定树(decision tree)中决策树归纳算法(ID3)解决机器学习中的回归问题.文中介绍基于有监督的学习方式,如何利用年龄.收入.身份.收入.信用等级等特征值来判定用户 ...
- String s=new String("abc")产生了几个对象?[权威面试版]
以下总结是我逛论坛 将零零碎碎的知识整理起来,方便自己记忆和阅读,顺便分享出来给大家学习. 若 String s=new String("abc"); 为第一句代码 则会产生两个对 ...
- JAVA_SE基础——19.数组的定义
数组是一组相关数据的集合,数组按照使用可以分为一维数组.二维数组.多维数组 本章先讲一维数组 不同点: 不使用数组定义100个整形变量:int1,int2,int3;;;;;; 使用数组定义 int ...