【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'组成的最大正方形.恩, 就是这样. 我们看到 ...
随机推荐
- 不能从const char *转换为LPCWSTR --VS经常碰到
不能从const char *转换为LPCWSTR 在VC 6.0中编译成功的项目在VS2005 vs2005.vs2008.vs2010中常会出现类型错误. 经常出现的错误是:不能从const ch ...
- Openstack中为虚拟机使用CDROM光驱设备
在Libvirt里处理 在nova里处理 实际效果 怎么卸载 在Libvirt里处理 尝试了下面有几种方法,为虚拟机载入光盘文件: 1.使用ide方式挂载: virsh attach-disk {in ...
- List 转 ObservableCollection
ObservableCollection<UserInfo> oc = new ObservableCollection<UserInfo>(); ls.ForEach(x = ...
- Flask刷新问题
修改页面中内容,特别是图片后,总是刷新不了.调试时,我常常通过修改端口来解决,从80-99不断改. 服务器部署,也遇到同样问题,重启web服务器,重启计算机都不行,网页已经改过来了,但是图片还是老图片 ...
- "Value does not fall within the expected range" with managed metadata fields
From: http://geekswithblogs.net/claraoscura/archive/2011/01/21/143569.aspx The problem: I have an ...
- [Canvas]碰撞球 增加小球间碰撞检测
请点此下载代码并用Chrome或是Firefox打开index.html 图例: 代码: <!DOCTYPE html> <html lang="utf-8"&g ...
- guava 学习笔记 瓜娃(guava)的API快速熟悉使用
1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection API Guava Basic Utilities IO API C ...
- Dictionary应用
using System; using System.Collections.Generic; using System.Data; using System.Web; using System.We ...
- Getting in Line UVA 216
Getting in Line Computer networking requires that the computers in the network be linked. This pro ...
- Hibernate的配置中,c3p0连接池相关配置
一.配置c3p0 1.导入 hibernate-c3po连接池包,Maven地址是:http://mvnrepository.com/artifact/org.hibernate/hibernate- ...