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 ...
随机推荐
- LayoutTransition实现显示、隐藏动画
public class Main4Activity extends Activity { private TextView tv1; private Button button1; private ...
- Android logcat
logcat是Android中一个命令行工具,可以用于得到程序的log信息. 基本命令 logcat使用方法如下所示: [adb] logcat [<option>] ... [<f ...
- 关于一个软件ipa包的其他图片资源
有时候 当你打开一个ipa包内容的时候 可能会找不到全部的资源 所以你需要在github上下载一个插件 下载下来以后 运行一下 然后会出来这个 把ipa 文件 拖到里面 ...
- 如何让 UITableViewCell 中的 imageView 大小固定
UITableView可以算是使用频率最高的组件之一的,在开发过程中经常需要展示一些简单的信息列表常见列表布局 如图,很多页面其实就是这种展示结果,通常需要imageView,textLabel,de ...
- Objective-C之Protocol
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- Swift中的类和结构体的相同点与不同点
相同点: 1.都是有内部变量和函数 2.都可以有内部下标方式去取属性 3.都可以有初始化函数 4.都可以用协议 不同点: 1.类有继承 2.类可以多重引用 3.类有析构
- eclipse执行单元测试报CreateProcess error=87的解决方法
原因是classpath的路径过长导致,在网上看了很多文章,发现解决方法有2种: 1.更改项目路径 或者 maven本地库的路径,减少classpath的深度. 2.由于这是eclipse自身的bug ...
- JQuery怎么实现页面刷新后保留鼠标点击样式的方法
今天抽空研究了下鼠标点击添加样式的方法.为了防止忘记,写篇文章算是备份吧. $('ul.main-menu li a').each(function(){ if($($(this))[0].h ...
- myeclipse10安装findbugs
尝试过myeclipse10环境下,在线安装findbugs,插件包是能下载到指定目录下,可是由于版本问题,findbugs插件是不能使用的.所以才有了下面的离线安装 离线安装findbugs 操作系 ...
- C#照片批量压缩小工具
做了一个照片批量压缩工具,其实核心代码几分钟就完成了,但整个小工具做下来还是花了一天的时间.中间遇到了大堆问题,并寻求最好的解决方案予以解决.现在就分享一下这个看似简单的小工具所使用的技术. 软件界面 ...