在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。

示例:

输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4

方法一:暴力(肯定超时)

方法二:

dp,

可以把dp[i][j]表示到i,j为止的正方形边长大小。

到i ,j的正方形大小是min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1。最小的那个就是正方形的最大边长大小

class Solution {
public:
int maximalSquare(vector<vector<char> >& matrix)
{
int r = matrix.size();
if(r == 0)
return 0;
int c = matrix[0].size();
vector<vector<int> > dp(r, vector<int>(c, 0));
int MAX = 0;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(i == 0 || j == 0)
{
dp[i][j] = matrix[i][j] - '0';
MAX = max(MAX, dp[i][j]);
continue;
}
else if(matrix[i][j] == '1')
{
dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
MAX = max(MAX, dp[i][j]);
}
}
}
return MAX * MAX;
}
};

Leetcode221. Maximal Square最大正方形的更多相关文章

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

  2. [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 ...

  3. [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 ...

  4. 221 Maximal Square 最大正方形

    在一个由0和1组成的二维矩阵内,寻找只包含1的最大正方形,并返回其面积.例如,给出如下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 4. 详见:https://l ...

  5. leetcode221 Maximal Square

    思路: dp. 实现: class Solution { public: int maximalSquare(vector<vector<char>>& matrix) ...

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

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

  7. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  8. 【动态规划】leetcode - Maximal Square

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

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

随机推荐

  1. Ubuntu环境下使用npm编译从git上clone下来的前端(Javascript)项目

    一.更新Ubuntu软件源 打开终端依次输入: $ sudo apt-get update $ sudo apt-get install -y python-software-properties s ...

  2. No converter found for return value of type: class com.alibaba.fastjson.JSON解决办法

    默认情况下,springMVC的@ResponseBody返回的是String类型,如果返回其他类型则会报错.使用fastjson的情况下,在springmvc.xml配置里加入: <mvc:a ...

  3. 代码风格JavaScript standard style与Airbnb style

    代码风格JavaScript  standard style与Airbnb style

  4. 1.前端数据可视化插件:Highcharts、Echarts和D3(区别)

    前端数据可视化插件有很多,但我用过的只有Highcharts(https://www.hcharts.cn/).Echarts(http://echarts.baidu.com/)和D3(https: ...

  5. 廖雪峰Java14Java操作XML和JSON-1XML-3SAX

    SAX:Simple API for XML 基于事件的API import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXPars ...

  6. ActiveMQ 传输协议

    配置 ActiveMQ安装目录的conf/activemq.xml中的<transportConnectors>标签之内. 配置示例 TCP(默认协议,性能相对可靠) Transmissi ...

  7. 洛谷P2526 【SHOI2001】小狗散步

    原题传送门 题目背景 Grant喜欢带着他的小狗Pandog散步.Grant以一定的速度沿着固定路线走,该路线可能自交.Pandog喜欢游览沿途的景点,不过会在给定的N个点和主人相遇.小狗和主人同时从 ...

  8. some方法过滤

    // 已经存在该tab时跳过 this.tabs.some(item => item.title === option.title) || this.tabs.push(option)

  9. 如何在window和mac下查找数据库

    1. mac 下终端使用步骤 cd /Applications/xampp/bin ./mysql -u root 2. window CMD命令中执行步骤 D: cd  D:/xampp/mysql ...

  10. JDK源码阅读--Object

    在java.lang包下 Object类:是所有类的基类(父类) public final native Class<?> getClass(); 返回这个Object所代表的的运行时类 ...