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的时候经过 ...
随机推荐
- FastAdmin 中 Bootstrap-Table 列宽控制
FastAdmin 中 Bootstrap-Table 列宽控制 使用 css 控制 1 使用 formatter 处理 2 http://issues.wenzhixin.net.cn/bootst ...
- Apache Kafka 0.9消费者客户端
当Kafka最初创建时,它与Scala生产者和消费者客户端一起运送.随着时间的推移,我们开始意识到这些API的许多限制.例如,我们有一个“高级”消费者API,它支持消费者组并处理故障转移,但不支持许多 ...
- Unit03: 容器对路径的处理 、 Servlet特性
Unit03: 容器对路径的处理 . Servlet特性 案例一:查询,增加员工: 重定向 处理请求资源路径 目录结构: 案例代码: package dao; import java.io.Seria ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料汇总 (上)
转载:http://dataunion.org/8463.html?utm_source=tuicool&utm_medium=referral <Brief History of Ma ...
- Java中的强制类型转换
例如,当程序中需要将 double 型变量的值赋给一个 int 型变量,该如何实现呢? 显然,这种转换是不会自动进行的!因为 int 型的存储范围比 double 型的小.此时就需要通过强制类型转换来 ...
- JS回调函数(深入篇)
<有些错别字> 在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中 ...
- WP8 MVVM设计模式
类似了Android里边的MVC模式, Windows Phone 有自己的Model-View-ViewModel模式,这种模式的作用就是为了Data和UI分离开来. 如果你英文较好的话,你可以不再 ...
- 堆、栈、free
转自:http://codeup.org/archives/212 http://bbs.bccn.net/thread-82212-1-1.html http://www.cppblog.com/o ...
- IDA Pro 权威指南学习笔记(十三) - 基本代码转换
IDA提供的代码转换包括: 1.将数据转换为代码 2.将代码转换为数据 3.指定一个指令序列为函数 4.更改现有函数的起始或结束地址 5.更改指令操作数的显示格式 代码显示选项 通过 Options ...
- BIOS设置图解教程-看完就没有不明白的了
BIOS(基本输入/输出系统)是被固化在计算机CMOS RAM芯片中的一组程序,为计算机提供最初的.最直接的硬件控制.BIOS主要有两类∶AWARD BIOS和AMI BIOS.正确设置BIOS可大大 ...