题目:

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 (最大的正方形)的更多相关文章

  1. 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 ...

  2. 【刷题-LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  3. 【LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  4. 【LeetCode】221. Maximal Square 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...

  5. 50.Maximal Square(最大正方形)

    Level   Medium 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square conta ...

  6. 【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 ...

  7. LeetCode OJ:Maximal Rectangle(最大矩形)

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  8. 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 ...

  9. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

随机推荐

  1. 转:Flash 插件面板 DragonBonesDesignPanel 的绿色安装方法

    最近在cocos2d-js下捣腾Dragonbones.转一个文章,大家可以参考安装Dragonbones.关于这个Dragonbones,5月份的时候还用得好好的,cocos2d-js还能妥妥的加载 ...

  2. 【树莓派+.NET MF打造视频监控智能车】遥控篇

    树莓派是最近比较火热的开源硬件,其设备只有信用卡大小,运行着Linux系统,专为学生编程教育而设计.我十多年的技术路线基本以学习微软的技术为主,中间也曾试图学习过linux,但是相对陡峭的学习曲线,只 ...

  3. checkbox 多选框 :jquery之全选、全不选、反选

    javascriptjqueryselectAll [html] view plaincopy   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...

  4. SpringBoot中mybatis配置自动转换驼峰标识没有生效

    mybatis提供了一个配置: #开启驼峰命名转换 mybatis.configuration.map-underscore-to-camel-case=true 使用该配置可以让mybatis自动将 ...

  5. RHEL7 配置临时IP 测试

    RHEL7 配置或添加临时IP地址测试: [root@rhel7 Desktop]# ip a s enp0s3 ---查看原网卡IP 为192.168.1.7 : enp0s3: <BROAD ...

  6. ACE的源码划分

    前几篇文章也提到过,ACE的所有源文件和头文件都杂乱堆在了ACE_wrappers/ace目录下.这样的代码组织方式给学习ACE带来了很大的困难,很多朋友在看到ace目录下庞大的代码的时候,几乎就失去 ...

  7. linux(ubuntu) 查看系统设备信息 命令

    时间:2012-08-02 00:12   ubuntu查看版本命令 方法一: 在终端中执行下列指令: cat /etc/issue 方法二: 使用 lsb_release 命令也可以查看 Ubunt ...

  8. Iterm2的一些好用法

    今天把mac带到公司办公了,爽歪歪啊. 1,如何脱离鼠标拷贝屏幕中的内容 1) Command+f  调出选择框,并在其中输入要复制的字符,可以使用Tab补全 2) 按 Command + c复制字符 ...

  9. warning C4005 DXGI_STATUS_OCCLUDED

    注意包含顺序 要将 $(DXSDK_DIR)Include放在后面才行 $(VC_IncludePath)$(WindowsSDK_IncludePath)$(DXSDK_DIR)Include 为了 ...

  10. 使用安全rm

    rm命令像一把刀子一样,玩不好会伤到自己.不要觉得自己头脑清醒,人总有犯迷糊的时候. 在.bashrc中设置PATH=/home/me/bin/:$PATH 在自己的bin目录下,添加rm脚本mv - ...