750. 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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
已经中毒了,感觉什么都要用dp:能数学直接解决的话就不麻烦了
[英文数据结构或算法,为什么不用别的数据结构或算法]:
没有,就是直接在矩阵上数
[一句话思路]:
找矩形就是找两行+两列,拆开
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
每一对 i j 算一次,所以res += count*(count - 1) / 2应该在j更新的紧随其后
[总结]:
可以用数学就不用强行套用dp
[复杂度]:Time complexity: O(n^3) Space complexity: O(1)
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int countCornerRectangles(int[][] grid) {
//cc: null
if (grid == null || grid.length == 0) return 0;
int res = 0;
int count = 0;
//for loop: 2 rows, count = cols
for (int i = 0; i < grid.length - 1; i++) {
for (int j = i + 1; j < grid.length; j++) {
count = 0;
for (int k = 0; k < grid[0].length; k++) {
if (grid[i][k] == 1 && grid[j][k] == 1) count++;
}
if (count > 1) res += count * (count - 1) / 2;
}
}
return res;
}
}
750. 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 ...
- [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 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- [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 ...
- [ACM_暴力][ACM_几何] ZOJ 1426 Counting Rectangles (水平竖直线段组成的矩形个数,暴力)
Description We are given a figure consisting of only horizontal and vertical line segments. Our goal ...
- 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少
[抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pr ...
- 191. Number of 1 Bits 二进制中1的个数
[抄题]: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...
- PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)
1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backw ...
- Soldier and Number Game---cf546D(打表求n的素因子个数)
题目链接:http://codeforces.com/problemset/problem/546/D 题意: 给出一个n,n开始是a!/b!,每次用一个x去整除n得到新的n,最后当n变成1的时候经过 ...
随机推荐
- 如何点焊过的镍片再次焊接到 PCBA 上?
如何将点焊过的镍片再次焊接到 PCBA 上? 在 PCBA 上贴了镍片再点焊,这样的制造工艺可以大大减少人工处理,提高生产通过率. 由于种种原因,有些机器可能有故障需要维修,而且电池又需要拆下来,才能 ...
- bzoj 3083 遥远的国度——树链剖分+线段树维护子树信息
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3083 int 的范围是 2^31 - 1 ,所以权值是不是爆 int 了…… O( nlog ...
- ABP学习之路
ABP文档介绍 http://www.cnblogs.com/kid1412/p/AbpDocumentContent.html [ABP开源项目]--vue+vuex+vue-router+EF的权 ...
- MFC程序如何修改icon图标
场景: Visual Studio写MFC应用程序,默认的程序左上角图标是自带的(如下图),虽说也不丑,但是对于程序员来说,还是缺乏个性了. 你知道,C.C++.java系程序员最常干的事情就是定义. ...
- AT命令控制上网 PDP
1. AT+CGATT=1 (Attach or detach from GPRS service, GPRS 附着状态) 说的简单点,这一步就是让SGSN (服务GPRS节点,你可以把它理解成与基站 ...
- socket执行accept函数时没有进入阻塞状态,而是陷入了无限循环
接着前两天继续看<VC深入详解>的网络编程部分,这次我快速看了遍书上的函数以及套接字C-S模型,然后自己从0开始写了个简单的服务端,结果发现一直在输出 而明明我还没有写客户端程序,由于打印 ...
- 纯css实现点击事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- rsync之脑袋疼
rsync图片参考 d本地模式,cp的感觉 vzrtopg = a - d -l --delete适用于2个目录完全一样的情况 默认avz就可以了 2,远端的shell 解决ssh链接慢的问题 3.d ...
- TCP三次握手和四次挥手、HTTP协议
TCP三次握手和四次挥手 首先我们知道HTTP协议通常承载于TCP协议之上,HTTPS承载于TLS或SSL协议层之上 通过上面这张图我们能够知道. 在Http工作之前,Web浏览器通过网络和W ...
- java后台读取配置文件中key与value -----demo
public class ResourcesUtils { /* * @description:根据属性获取文件名 * * @param:propertyName文件的属性名 * * @return: ...