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 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.
题目链接:https://leetcode.com/problems/maximal-square/。
这一题有点相似:LeetCode OJ 之 Maximal Rectangle (最大的矩形)。可是解题方法全然不同。
思路:
动态规划。设f[i][j]表示包含当前点的正方形的最大变长,有递推关系例如以下:
f[0][j] = matrix[0][j]
f[i][0] = matrix[i][0]
For i > 0 and j > 0:
if matrix[i][j] = 0, f[i][j] = 0;
if matrix[i][j] = 1, f[i][j] = min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1.
代码1:
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix)
{
int row = matrix.size();
if(row == 0)
return 0;
int col = matrix[0].size();
vector<vector<int> > f(row , vector<int>(col , 0));
int maxsize = 0; //最大边长
for(int i = 0 ; i < row ; i++)
{
for(int j = 0 ; j < col ; j++)
{
if(i == 0 || j == 0)
f[i][j] = matrix[i][j]-'0';
else
{
if(matrix[i][j] == '0')
f[i][j] = 0;
else
f[i][j] = min(min(f[i-1][j] , f[i][j-1]) , f[i-1][j-1]) + 1;
}
maxsize = max(maxsize , f[i][j]);
}
}
return maxsize * maxsize;
}
};
代码2:
优化空间为一维
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix)
{
int row = matrix.size();
if(row == 0)
return 0;
int col = matrix[0].size();
vector<int> f(col , 0);
int tmp1 = 0 , tmp2 = 0;
int maxsize = 0; //最大边长
for(int i = 0 ; i < row ; i++)
{
for(int j = 0 ; j < col ; j++)
{
tmp1 = f[j]; //tmp1把当前f[j]保存以下,用来做下一次推断f[i+1][j+1]的左上角f[i-1][j-1]
if(i == 0 || j == 0)
f[j] = matrix[i][j]-'0';
else
{
if(matrix[i][j] == '0')
f[j] = 0;
else
f[j] = min(min(f[j-1] , f[j]) , tmp2) + 1; //这里的tmp2即是代码1的f[i-1][j-1]
}
tmp2 = tmp1 ; //把tmp1赋给tmp2,用来下次for循环求f[j+1]
maxsize = max(maxsize , f[j]);
}
}
return maxsize * maxsize;
}
};LeetCode OJ 之 Maximal Square (最大的正方形)的更多相关文章
- 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 ret ...
- 【刷题-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】221. Maximal Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...
- 50.Maximal Square(最大正方形)
Level Medium 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square conta ...
- 【LeetCode 221】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 OJ:Maximal Rectangle(最大矩形)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- LeetCode OJ 85. Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
随机推荐
- eclipse mavenWeb项目真正实现热部署(修改java代码和页面文件不用重启tomcat)
1.前言 首先,本文创作灵感源于博客园园作者signheart,特此鸣谢!原文链接见文末推荐: 百度都搜破了,全网讲的都是如何将maven项目部署到tomcat上,对于热部署的认知,真 ...
- 【DB2】监控临时表空间使用
在我们使用数据库的时候,我们都知道应用程序在DB2上运行时,会产生临时表空间,我们想要监测这些临时表空间的使用情况,可以使用以下步骤: (1)打开monitor switches 中的table监视器 ...
- SpringCloud stream连接RabbitMQ收发信息
百度上查的大部分都是一些很简单的单消费者或者单生产者的例子,并且多是同一个服务器的配置,本文的例子为多服务器配置下的消费生产和消费者配置. 参考资料:https://docs.spring.io/sp ...
- 转 configure: error: *** libmcrypt was not found解决方案
安装到mcrypt的时候出现了问题./configure提示出错,首先提示*** Could not run libmcrypt test program, checking why…*** The ...
- 完美解决office2013 错误1402
遇到1402问题 按照网络上的帖子都无法解决,老提示无权限更改,原来只是少了一个步骤而已!经本人多次试验,已经完美解决,现在上图! 步骤 肯定是得先出现错误,找到注册表所在项! 这个就不赘述,通过 ...
- ios中图片旋转
@interface ViewController () { UIImageView *_imageview; BOOL flag; } @end @implementation ViewContro ...
- eclipse的jdk版本和spring冲突问题WARN XmlWebApplicationContext:1060 - Exception thrown from LifecycleProcessor on context close
项目环境: jdk1.8 tomcat7 问题:eclipse启动tomcat后控制台报如下错误: WARN XmlWebApplicationContext:1060 - Exception thr ...
- SQL之group by
转自:理解group by 先来看下表1,表名为test: 表1 执行如下SQL语句: 1 2 SELECT name FROM test GROUP BY name 你应该很容易知道运行的结果,没错 ...
- STRTOK函数和STRTOK_R函数
STRTOK函数和STRTOK_R函数 注:本文转载自博客园,感谢作者整理! 1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct pe ...
- vim缩进参考线
编辑缩进嵌套的文件时想找到对应的层级比较困难,写了一个函数,使用cc选项设定一条辅助线,标识到指定的缩进层级.代码如下: ? ReferenceLine 1 2 3 4 5 6 7 8 9 10 11 ...