[抄题]:

Given a grid where each entry is only 0 or 1, find the number of corner rectangles.

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:

  1. Input: grid =
  2. [[1, 0, 0, 1, 0],
  3. [0, 0, 1, 0, 1],
  4. [0, 0, 0, 1, 0],
  5. [1, 0, 1, 0, 1]]
  6. Output: 1
  7. Explanation: There is only one corner rectangle, with corners grid[1][2], grid[1][4], grid[3][2], grid[3][4].

Example 2:

  1. Input: grid =
  2. [[1, 1, 1],
  3. [1, 1, 1],
  4. [1, 1, 1]]
  5. Output: 9
  6. Explanation: There are four 2x2 rectangles, four 2x3 and 3x2 rectangles, and one 3x3 rectangle.

Example 3:

  1. Input: grid =
  2. [[1, 1, 1, 1]]
  3. Output: 0
  4. 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给出的题目变变变]:

[代码风格] :

  1. class Solution {
  2. public int countCornerRectangles(int[][] grid) {
  3. //cc: null
  4. if (grid == null || grid.length == 0) return 0;
  5.  
  6. int res = 0;
  7. int count = 0;
  8.  
  9. //for loop: 2 rows, count = cols
  10. for (int i = 0; i < grid.length - 1; i++) {
  11. for (int j = i + 1; j < grid.length; j++) {
  12. count = 0;
  13. for (int k = 0; k < grid[0].length; k++) {
  14. if (grid[i][k] == 1 && grid[j][k] == 1) count++;
  15. }
  16. if (count > 1) res += count * (count - 1) / 2;
  17. }
  18. }
  19.  
  20. return res;
  21. }
  22. }

750. Number Of Corner Rectangles四周是点的矩形个数的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 【LeetCode】750. Number Of Corner Rectangles 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  4. [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 ...

  5. [ACM_暴力][ACM_几何] ZOJ 1426 Counting Rectangles (水平竖直线段组成的矩形个数,暴力)

    Description We are given a figure consisting of only horizontal and vertical line segments. Our goal ...

  6. 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少

    [抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pr ...

  7. 191. Number of 1 Bits 二进制中1的个数

    [抄题]: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...

  8. PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

    1024 Palindromic Number (25 分)   A number that will be the same when it is written forwards or backw ...

  9. Soldier and Number Game---cf546D(打表求n的素因子个数)

    题目链接:http://codeforces.com/problemset/problem/546/D 题意: 给出一个n,n开始是a!/b!,每次用一个x去整除n得到新的n,最后当n变成1的时候经过 ...

随机推荐

  1. curl 无法访问 https 协议

    转自http://blog.mutoo.im/2013/12/curl-could-not-communicate-with-https-sites.html mac升级为10.10以后,homebr ...

  2. Pdnovel 在线阅读体验

    pdnovel是discuz的一款小说阅读插件,本身是用php开发的,数据存储于mysql,小说文本存储于file文件.pdnovel本身已有添加书籍.连载章节的功能,但为了批量添加全本txt书籍又开 ...

  3. h264 aac 封装 flv

    Part 1flvtag组成 FLV 文件结构由 FLVheader和FLVBody组成.(注意flv文件是大端格式的)FLV头组成(以c为例子,一字节对齐):FLVBody是由若干个Tag组成的:  ...

  4. appium+python自动化28-name定位

    前言 appium1.5以下老的版本是可以通过name定位的,新版本从1.5以后都不支持name定位了 name定位报错 1.最新版appium V1.7用name定位,报错: selenium.co ...

  5. STL算法与树结构模板

    STL算法 STL 算法是一些模板函数,提供了相当多的有用算法和操作,从简单如for_each(遍历)到复杂如stable_sort(稳定排序),头文件是:#include <algorithm ...

  6. cx_Oracle.DatabaseError: ORA-12541: TNS:no listener

    问题:利用Python连接Oracle时报错,完整过程如下 import cx_Oracle conn = cx_Oracle.connect('testma/dingjia@192.168.88.1 ...

  7. python学习(二十七) 元组

    # 元组是不可变的,不能改变元素的值,也不能增加.减少元素my_tuple = (1, 2, 3, 3)print(my_tuple) # 查找元素位置print(my_tuple.index(2)) ...

  8. socket编程之select()

    int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout); 参数 ...

  9. linux oracle服务器无密码登录dba

    1.su - oracle 切换到oracle 2.sqlplus sys/manger as sysdba 3.新建用户: create user username identified by pa ...

  10. 【BZOJ】2342: [Shoi2011]双倍回文(Manacher)

    题目 传送门:QWQ 分析 (sb如我写了发不知道什么东西在洛谷上竟然水了84分 嗯咳 设$ i $为双重回文的中心 如果$ j~i $ 可以被算作答案,只有满足如下两式: $ p[j]+j \geq ...