[LeetCode] Maximal Square 最大正方形
2015-06-03 23:22
501 查看
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.
For example, given the following matrix:
Return 4.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
这道题我刚看到的时候,马上联想到了之前的一道Number of Islands 岛屿的数量,但是仔细一对比,发现又不太一样,那道题1的形状不确定,很适合DFS的特点,而这道题要找的是正方形,是非常有特点的形状,所以并不需要用到DFS,要论相似,我到认为这道Maximal Rectangle 最大矩形更相似一些。这道题的解法不止一种,我们先来看一种brute force的方法,这种方法的机理就是就是把数组中每一个点都当成正方形的左顶点来向右下方扫描,来寻找最大正方形。具体的扫描方法是,确定了左顶点后,再往下扫的时候,正方形的竖边长度就确定了,只需要找到横边即可,这时候我们使用直方图的原理,从其累加值能反映出上面的值是否全为1,之前也有一道关于直方图的题Largest Rectangle in Histogram 直方图中最大的矩形 。通过这种方法我们就可以找出最大的正方形,参见代码如下:
解法一
参考资料:
https://leetcode.com/discuss/38485/my-concise-solution-in-c
https://leetcode.com/discuss/38489/easy-solution-with-detailed-explanations-8ms-time-and-space
http://www.5uzh.com/view.aspx?kn=50037263
LeetCode All in One 题目讲解汇总(持续更新中...)
For example, given the following matrix:
1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0
Return 4.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
这道题我刚看到的时候,马上联想到了之前的一道Number of Islands 岛屿的数量,但是仔细一对比,发现又不太一样,那道题1的形状不确定,很适合DFS的特点,而这道题要找的是正方形,是非常有特点的形状,所以并不需要用到DFS,要论相似,我到认为这道Maximal Rectangle 最大矩形更相似一些。这道题的解法不止一种,我们先来看一种brute force的方法,这种方法的机理就是就是把数组中每一个点都当成正方形的左顶点来向右下方扫描,来寻找最大正方形。具体的扫描方法是,确定了左顶点后,再往下扫的时候,正方形的竖边长度就确定了,只需要找到横边即可,这时候我们使用直方图的原理,从其累加值能反映出上面的值是否全为1,之前也有一道关于直方图的题Largest Rectangle in Histogram 直方图中最大的矩形 。通过这种方法我们就可以找出最大的正方形,参见代码如下:
解法一
class Solution { public: int maximalSquare(vector<vector<char> >& matrix) { int res = 0; for (int i = 0; i < matrix.size(); ++i) { vector<int> v(matrix[i].size(), 0); for (int j = i; j < matrix.size(); ++j) { for (int k = 0; k < matrix[j].size(); ++k) { if (matrix[j][k] == '1') ++v[k]; } res = max(res, getSquareArea(v, j - i + 1)); } } return res; } int getSquareArea(vector<int> &v, int k) { if (v.size() < k) return 0; int count = 0; for (int i = 0; i < v.size(); ++i) { if (v[i] != k) count = 0; else ++count; if (count == k) return k * k; } return 0; } };
参考资料:
https://leetcode.com/discuss/38485/my-concise-solution-in-c
https://leetcode.com/discuss/38489/easy-solution-with-detailed-explanations-8ms-time-and-space
http://www.5uzh.com/view.aspx?kn=50037263
LeetCode All in One 题目讲解汇总(持续更新中...)
相关文章推荐
- 【黑马程序员】---java基础---内部类、异常
- javascript 正则表达式提取数字使用
- Cannot refer to a non-final variable mylis inside an inner class defined in a different method
- hive的Specified key was too long; max key length is 767 bytes问题解决
- 广播机制(二)
- windows server 2008 iis 搭建asp
- iPhone 6s 要删除核心功能了,你希望是哪一项?
- HDU2036 改革春风吹满地(求多变形面积)
- HTTP简单介绍
- CDH集群集成kafka
- 文字在div中垂直居中的方法,设置div的高度height和行高line-height一致
- AngularJS笔记---数据绑定
- http服务(四)――apache虚拟主机配置
- 服务器端表格控件嵌套HTML元素获取解决方案
- 西普学院Crypto之凯撒是罗马共和国杰出的军事统帅
- linux中的vi命令
- Android Data Binding 数据绑定技术导读
- opengl 机器人手臂
- 常用 SQL 随笔
- linux中的vi命令