【LeetCode】221. Maximal Square
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.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
这题的DP思想部分借鉴了jianchao.li.fighter。
思路如下:构建二维数组len,len[i][j]表示以(i,j)为右下角的最大方块的边长。
递推关系为 len[i][j] = min(min(len[i-1][j], len[i][j-1]), len[i-1][j-1]) + 1;
如下图示意:

以(i,j)为右下角的最大方块边长,取决于周围三个位置(i-1,j),(i,j-1),(i-1,j-1),恰好为三者最小边长扩展1位。
若三者最小边长为0,那么(i,j)自成边长为1的方块。
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if(matrix.empty() || matrix[].empty())
return ;
int m = matrix.size();
int n = matrix[].size();
int maxLen = ;
vector<vector<int> > len(m, vector<int>(n, ));
// first row
for(int i = ; i < n; i ++)
{
len[][i] = (int)(matrix[][i]-'');
if(len[][i] == )
maxLen = ;
}
// first col
for(int i = ; i < m; i ++)
{
len[i][] = (int)(matrix[i][]-'');
if(len[i][] == )
maxLen = ;
}
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(matrix[i][j] == '')
len[i][j] = ;
else
{
len[i][j] = min(min(len[i-][j], len[i][j-]), len[i-][j-]) + ;
maxLen = max(len[i][j], maxLen);
}
}
}
return maxLen * maxLen;
}
};

【LeetCode】221. Maximal Square的更多相关文章
- 【LeetCode】221. Maximal Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】85. Maximal Rectangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...
- 【LeetCode】85. Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【leetcode】85. Maximal Rectangle(单调栈)
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...
- 【LeetCode】085. Maximal Rectangle
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's ...
- 【LEETCODE】69、动态规划,easy,medium级别,题目:198、139、221
package y2019.Algorithm.dynamicprogramming.easy; /** * @ProjectName: cutter-point * @Package: y2019. ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
随机推荐
- Ios开发之多线程编程——NSThread
IOS程序在运行的时候是通过主线程来进行UI视图的更新和响应屏幕触摸事件,但是,在视图更新的时候,会有一些非常耗时的工作,这样我们会出现系统出现卡顿的现象,这是因为主线程堵塞造成的,这样会使用户体验非 ...
- 使用JDBC向Kudu表插入中文数据乱码(转载)
参考:https://cloud.tencent.com/developer/article/1077763 问题描述 使用Impala JDBC向Kudu表中插入中文字符,插入的中文字符串乱码,中文 ...
- Python实现爬虫设置代理IP和伪装成浏览器的方法(转载)
https://www.jb51.net/article/139587.htm chrome_options = webdriver.ChromeOptions() chrome_options.ad ...
- 定时删除日志文件---linux定时清理日志
linux是一个很能自动产生文件的系统,日志.邮件.备份等.虽然现在硬盘廉价,我们可以有很多硬盘空间供这些文件浪费,让系统定时清理一些不需要的文件很有一种爽快的事情.不用你去每天惦记着是否需要清理日志 ...
- 【Kafka】Kafka为什么要加入分区的概念
Kafka为什么要加入分区的概念 kafka 分区 作用_百度搜索 (1 封私信)kafka中的topic为什么要进行分区? - 知乎 Kafka安装版本选择 Apache Kafka nc使用 n ...
- Docker Inspect
1.Inspect结果详细信息 docker inspect 7988f914a122 其中7988f914a122是某一容器进程的id { "Id": "7988f91 ...
- C118+OSMCOMBB嗅探短信
ubuntu系统:12.04.4, 下载地址:http://cdimage.ubuntu.com/releases/12.04.4/release/ 编译环境下载 : http://pan.baidu ...
- JQuery为textarea添加maxlength
<html> <head> <title>JQuery为textarea添加maxlength</title> <script type=&quo ...
- 微信小程序 - 支持html空格(提示)
仅限于text标签,decode参数:官方api.
- 012-Go ORM框架之Gorm测试
1:参考:https://github.com/jinzhu/gorm 2:数据库脚本(pg) -- create table posts( id serial primary key, conten ...