Maximal Square
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:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int n=matrix.size();
if(n<) return ;
int m=matrix[].size();
if(m<) return ;
vector<vector<int>> size(n,vector<int>(m,));
int maxnum=;
int i;
for(i=;i<n;i++)
{
if(matrix[i][]=='')
{
size[i][]=;
maxnum=;
}
}
for(i=;i<m;i++)
{
if(matrix[][i]=='')
{
size[][i]=;
maxnum=;
}
}
int j;
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(matrix[i][j]=='')
{
size[i][j]=min(size[i-][j-],min(size[i-][j],size[i][j-]))+;
if(maxnum<size[i][j])
maxnum=size[i][j];
}
else
size[i][j]=;
}
}
return maxnum*maxnum;
}
};
Maximal Square的更多相关文章
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- [LintCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 【动态规划】leetcode - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- [LeetCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- LeetCode Maximal Square
原题链接在这里:https://leetcode.com/problems/maximal-square/ 这是一道DP题,存储历史信息是到当前点能有的最大square, 用二维数组dp存储. 更新方 ...
- LeetCode OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
随机推荐
- Python: PDB命令
1. where(w) 找出当前代码运行位置 2. list(l) 显示当前代码的部分上下文 3. list <line number> 显示指定行的上下文 4. list <lin ...
- 第一个JSP程序
本文介绍如何写出第一个JSP程序 1.配置服务器 (1)在eclipse中选择Server视图,(ps:很多童鞋说找不到Server,那是因为eclipse的版本问题,请下载JEE版本的eclipse ...
- storyBoard配置错误导致崩溃 superview]: unrecognized selector...
控制台打印崩溃原因 [TaskStartVC superview]: unrecognized selector sent to instance RT TaskStartVC是一个同storyBoa ...
- 为Xcode添加和备份快捷代码
有IOS开发经验的,相信你一定了解快捷代码的使用以及可以自定义代码,备份到XCode右下角,供下次使用. 那么,快捷代码备份到本地什么位置呢: 位置:~/Library/Developer/Xcode ...
- DEV主从表
1.主从表隐藏表格展开按钮. 当主表内容不包含子表时候隐藏,主从表加号图标.效果如下图. 实现代码 private void gvMain_CustomDrawCell(object sender, ...
- C#中Dictionary,Hashtable,List的比较及分析
一. Dictionary与Hashtable Dictionary与Hashtable都是.Net Framework中的字典类,能够根据键快速查找值 二者的特性大体上是相同的,有时可以把Dicti ...
- cocos2d-x之加速度传感器初试
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Device::setAccelerometerEnabled(tr ...
- opencv3.1 + opencv_contrib编译记事(win7下)
折腾了好几天,终于把opencv3.1加上一个额外的包opencv_contrib编译好了.(总体来说编译opencv就是填坑!!!) 最后我编译成功的是mingw版本的.也就是结合了Qt4.7+cm ...
- HDU 2189 悼念512汶川大地震遇难同胞――来生一起走 --生成函数
这题跟上两题也差不多. 把150以内的素数找出来,把素数的值看做硬币的面值,每个硬币的个数即ceil(150/prime[i]),因为再多也没用,最多组成n=150就行了,所以又回到了找硬币问题.用生 ...
- Unity4.6新UI系统初探(uGUI)
一.引言 Unity终于在即将到来的4.6版本内集成了所见即所得的UI解决方案(视频).事实上从近几个版本开始,Unity就在为这套系统做技术扩展,以保证最终能实现较理想的UI系统.本文试图通过初步的 ...