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.

  1. Example 1:
  2. Input: grid =
  3. [[1, 0, 0, 1, 0],
  4. [0, 0, 1, 0, 1],
  5. [0, 0, 0, 1, 0],
  6. [1, 0, 1, 0, 1]]
  7. Output: 1
  8. Explanation: There is only one corner rectangle, with corners grid[1][2], grid[1][4], grid[3][2], grid[3][4].
  9. Example 2:
  10. Input: grid =
  11. [[1, 1, 1],
  12. [1, 1, 1],
  13. [1, 1, 1]]
  14. Output: 9
  15. Explanation: There are four 2x2 rectangles, four 2x3 and 3x2 rectangles, and one 3x3 rectangle.
  16. Example 3:
  17. Input: grid =
  18. [[1, 1, 1, 1]]
  19. Output: 0
  20. Explanation: Rectangles must have four distinct corners.
  21. Note:
  22. The number of rows and columns of grid will each be in the range [1, 200].
  23. Each grid[i][j] will be either 0 or 1.
  24. The number of 1s in the grid will be at most 6000.
  25. Discuss

暴力枚举+轻微减枝

  1. class Solution {
  2. public:
  3. int search(vector<vector<int>>& grid, int x, int y) {
  4. int n = grid.size();
  5. int m = grid[0].size();
  6. int cnt = 0;
  7. for (int i = 1; i < n - x; ++i) {
  8. if(grid[x+i][y] == 0) continue;
  9. for (int j = 1; j < m - y; ++j) {
  10. int x1, y1;
  11. x1 = x;
  12. y1 = y + j;
  13. if (grid[x1][y1] == 0) continue;
  14. x1 = x + i;
  15. y1 = y + j;
  16. if (grid[x1][y1] == 0) continue;
  17. x1 = x + i;
  18. y1 = y;
  19. if (grid[x1][y1] == 0) continue;
  20. cnt++;
  21. }
  22. }
  23. return cnt;
  24. }
  25. int countCornerRectangles(vector<vector<int>>& grid) {
  26. int n = grid.size();
  27. int m = grid[0].size();
  28. if (n == 1) return 0;
  29. int ans = 0;
  30. for (int i = 0; i < n; ++i) {
  31. for (int j = 0; j < m; ++j) {
  32. if (grid[i][j] == 1) {
  33. ans += search(grid, i, j);
  34. }
  35. }
  36. }
  37. return ans;
  38. }
  39. };

leetcode 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 解题报告 (C++)

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

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

  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. C#版 - Leetcode 191. Number of 1 Bits-题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  6. [leetcode]200. Number of Islands岛屿个数

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  7. [leetcode]694. Number of Distinct Islands你究竟有几个异小岛?

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  8. [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  9. [LeetCode] 694. Number of Distinct Islands

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

随机推荐

  1. AC日记——Dylans loves tree hdu 5274

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  2. Win10下安装Docker及tensorflow(cpu版)

    1.准备工作: 1)64为操作系统,win7或者更高 2)支持“ Hardware Virtualization Technology”,并且,“virtualization ”可用(可进入任务管理器 ...

  3. k8s之存储卷及pvc

    1.存储卷概述 因为pod是有生命周期的,pod一重启,里面的数据就没了,所以我们需要数据持久化存储,在k8s中,存储卷不属于容器,而是属于pod,也就是说同一个pod中的容器可以共享一个存储卷,存储 ...

  4. Ural 1780 Gray Code 乱搞暴力

    原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780 1780. Gray Code Time limit: 0.5 secondMem ...

  5. Openlayers3 编辑要素

    参考文章 Openlayers之编辑要素 MAPZONE GIS SDK接入Openlayers3之五——图形编辑工具 [学习笔记之Openlayers3]要素保存篇(第四篇) openlayers实 ...

  6. iOS -- SKPhysicsJointSpring类

    SKPhysicsJointSpring类 继承自 NSObject 符合 NSCoding(SKPhysicsJoint)NSObject(NSObject) 框架  /System/Library ...

  7. 3D投影

    3D投影方式的几大种类: 1.快门式 主动快门式即时分式,不过我们通常用前面的叫法,快门式3D眼镜(3D Shutter Glasses,也称作LC shutter glassesor active  ...

  8. The bean 'xxx' could not be injected as a 'xxx'because it is a JDK dynamic proxy that implements

    启动springboot项目的时候示以下错误 Error starting ApplicationContext. To display the conditions report re-run yo ...

  9. LinearLayout具体解释三:LayoutInflater创建View过程分析

    上次讲到以下这么一段代码,这段代码的作用就是解析xml文件成为view并显示到屏幕上的. @Override //设置contentview,也就是activity或fragment载入视图,即vie ...

  10. Windows 系统 vs2012 MinGW 编译ffmpeg 静态库

    Windows系统下 vs2012编译ffmpeg 动态库 前面已经有文章讲述,本文将讲述如果编译生成ffmpeg静态库以方便 在vs2012下调用. 准备工作:安装MinGW环境,修改ffmpeg配 ...