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 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的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- AC日记——Dylans loves tree hdu 5274
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- Win10下安装Docker及tensorflow(cpu版)
1.准备工作: 1)64为操作系统,win7或者更高 2)支持“ Hardware Virtualization Technology”,并且,“virtualization ”可用(可进入任务管理器 ...
- k8s之存储卷及pvc
1.存储卷概述 因为pod是有生命周期的,pod一重启,里面的数据就没了,所以我们需要数据持久化存储,在k8s中,存储卷不属于容器,而是属于pod,也就是说同一个pod中的容器可以共享一个存储卷,存储 ...
- Ural 1780 Gray Code 乱搞暴力
原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780 1780. Gray Code Time limit: 0.5 secondMem ...
- Openlayers3 编辑要素
参考文章 Openlayers之编辑要素 MAPZONE GIS SDK接入Openlayers3之五——图形编辑工具 [学习笔记之Openlayers3]要素保存篇(第四篇) openlayers实 ...
- iOS -- SKPhysicsJointSpring类
SKPhysicsJointSpring类 继承自 NSObject 符合 NSCoding(SKPhysicsJoint)NSObject(NSObject) 框架 /System/Library ...
- 3D投影
3D投影方式的几大种类: 1.快门式 主动快门式即时分式,不过我们通常用前面的叫法,快门式3D眼镜(3D Shutter Glasses,也称作LC shutter glassesor active ...
- 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 ...
- LinearLayout具体解释三:LayoutInflater创建View过程分析
上次讲到以下这么一段代码,这段代码的作用就是解析xml文件成为view并显示到屏幕上的. @Override //设置contentview,也就是activity或fragment载入视图,即vie ...
- Windows 系统 vs2012 MinGW 编译ffmpeg 静态库
Windows系统下 vs2012编译ffmpeg 动态库 前面已经有文章讲述,本文将讲述如果编译生成ffmpeg静态库以方便 在vs2012下调用. 准备工作:安装MinGW环境,修改ffmpeg配 ...