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 either 0 or 1.
The number of 1s in the grid will be at most 6000.
Discuss

暴力枚举+轻微减枝

class Solution {
public:
int search(vector<vector<int>>& grid, int x, int y) {
int n = grid.size();
int m = grid[0].size();
int cnt = 0;
for (int i = 1; i < n - x; ++i) {
if(grid[x+i][y] == 0) continue;
for (int j = 1; j < m - y; ++j) {
int x1, y1; x1 = x;
y1 = y + j;
if (grid[x1][y1] == 0) continue; x1 = x + i;
y1 = y + j;
if (grid[x1][y1] == 0) continue; x1 = x + i;
y1 = y;
if (grid[x1][y1] == 0) continue;
cnt++;
}
}
return cnt;
}
int countCornerRectangles(vector<vector<int>>& grid) {
int n = grid.size();
int m = grid[0].size();
if (n == 1) return 0;
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (grid[i][j] == 1) {
ans += search(grid, i, j);
}
}
}
return ans;
}
};

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. python笔记3:注释命名风格

    6.注释: 行注释采用  # 开头,多行注释使用三个单引号 (''') 或三个双引号 ("' '"),注释不需要对齐 三引号让程序员从引号和特殊字符串的泥潭里面解脱出来,自始至终保 ...

  2. 找小于N 的所有质数

    笔试题目当中,找素数出现的几率有点大.昨天就做了一个,感觉不是很难,但可以考查程序员的数学和编码功底. 用嵌套循环来实现是很理想的,怎样减少循环的次数?怎样求出小于N的所有质数? 不可能将一个数除与所 ...

  3. python导入sklearn模块出现DLL load failed的解决办法

    笔者安装的python版本是2.7.6,最近在导入sklearn(版本:0.16.1)的模块时,经常出现DLL load failed的报错,具体截图如下: 解决办法与步骤如下: 由于sklearn的 ...

  4. 转 Tesseract-OCR 字符识别---样本训练

    转自:http://blog.csdn.net/feihu521a/article/details/8433077 Tesseract是一个开源的OCR(Optical Character Recog ...

  5. mysql读写分离的三种实现方式

    1 程序修改mysql操作类可以参考PHP实现的Mysql读写分离,阿权开始的本项目,以php程序解决此需求.优点:直接和数据库通信,简单快捷的读写分离和随机的方式实现的负载均衡,权限独立分配缺点:自 ...

  6. 漫谈深度学习时代点击率预估技术进展 &&深度学习在推荐系统上的发展

    转载:https://www.infoq.cn/article/XA055tpFrprUy*0UBdCb https://www.zhihu.com/question/20830906/answer/ ...

  7. C# 解析迅雷链接成正常的Http链接

    目前热门剧司马懿第一集的迅雷下载地址是: thunder://QUFodHRwOi8vZGwxMjIuODBzLmltOjkyMC8xNzA2L1vlpKflhptTU+mprOaHv+S5i+WGm ...

  8. 金山面试CDN

    History 今天去金山网络面试的时候,被问到性能优化,我说了几个.最后说到了CDN,我说要尽量把静态的内容放置到CDN,可是为什么呢?面试官说既然你说到CDN.你就说说它的原理. 之前有看过,可是 ...

  9. valid-palindrome——判断带符号数字字母的字符串是否为回文

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  10. vim 宏的使用

    1. 基本使用 q[a-z] 开始录制宏 q  停止录制 @[a-z] 使用宏 @@ 调用最近使用的宏 22@[a-z] 多次重放宏 2. 宏的执行方式 串行方式:5@[a-z] 宏内包含向下一个目标 ...