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

示例:

  1. 输入:
  2.  
  3. 1 0 1 0 0
  4. 1 0 1 1 1
  5. 1 1 1 1 1
  6. 1 0 0 1 0
  7.  
  8. 输出: 4
    解法:判断以某个点为正方形右下角时最大的正方形时,那它的上方,左方和左上方三个点也一定是某个正方形的右下角,否则该点为右下角的正方形最大就是它自己了。
    我们知道,该点为右下角的正方形的最大边长,最多比它的上方,左方和左上方为右下角的正方形的边长多1,最好的情况是是它的上方,左方和左上方为右下角的正方形的大小都一样的,这样加上该点就可以构成一个更大的正方形。
    但如果它的上方,左方和左上方为右下角的正方形的大小不一样,合起来就会缺了某个角落,这时候只能取那三个正方形中最小的正方形的边长加1了。
  1. class Solution {
  2. public:
  3. int maximalSquare(vector<vector<char>>& matrix) {
  4. if(matrix.empty() || matrix[].empty())
  5. return ;
  6. int n=matrix.size(),m=matrix[].size();
  7. int ans=;
  8. int dp[n][m];
  9. memset(dp,,sizeof(dp));
  10. for(int i=;i<n;i++)
  11. {
  12. if(matrix[i][]=='')
  13. {
  14. dp[i][]=;ans=;
  15. }
  16. }
  17. for(int i=;i<m;i++)
  18. {
  19. if(matrix[][i]=='')
  20. {
  21. dp[][i]=;ans=;
  22. }
  23. }
  24. for(int i=;i<n;i++)
  25. {
  26. for(int j=;j<m;j++)
  27. {
  28. if(matrix[i][j]=='')
  29. dp[i][j]=min(dp[i-][j-],min(dp[i-][j],dp[i][j-]))+;
  30. ans=max(ans,dp[i][j]);
  31. }
  32. }
  33. return ans*ans;
  34. }
  35. };

LeetCode 最大正方形的更多相关文章

  1. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  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] Valid Square 验证正方形

    Given the coordinates of four points in 2D space, return whether the four points could construct a s ...

  4. [LeetCode] Magic Squares In Grid 网格中的神奇正方形

    A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, co ...

  5. C#版 - Leetcode 593. 有效的正方形 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

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

  7. Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)

    Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...

  8. C#刷遍Leetcode面试题系列连载(5):No.593 - 有效的正方形

    上一篇 LeetCode 面试题中,我们分析了一道难度为 Easy 的数学题 - 自除数,提供了两种方法.今天我们来分析一道难度为 Medium 的面试题. 今天要给大家分析的面试题是 LeetCod ...

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

随机推荐

  1. icekingdom(2018.10.17)

    一句话题意:给你一颗n个点的树,节点初始状态下都是白色,有q次修改,每次修改会把[li,ri]区间内的点染成黑色,并且问黑色点能形成几个联通块,然后会将所有点染回白色.(也就是说每次都只有[li,ri ...

  2. Git - Merge: refusing to merge unrelated histories

    场景 我在本地有个代码仓库local-A,本地仓库local-A已经和一个远程仓库remote-A关联了. 接着我又在GitHub上新建了一个仓库remote-B,我希望将本地仓库local-A的本地 ...

  3. python连接mysql时连接不到test文件夹怎么办

    最新版mysql安装后默认是没有test文件夹的,这时候需要我们自己创建一个test文件夹, 文件默认路径如下    C:\ProgramData\MySQL\MySQL Server 5.7\Dat ...

  4. javascript 中not defined 和undefined有什么区别

    概念上的解释:undefined是javascript语言中定义的五个原始类中的一个,换句话说,undefined并不是程序报错,而是程序允许的一个值.not defined是javascript在运 ...

  5. python 基础(十五) socket编程

    SOCKET TCP协议: 有请求 有响应 称之为 tcp协议 是面向连接的协议 就是在收发数据之前 必须先要建立一个可靠的链接 三次握手 如:网站 UDP协议: 是一个非链接的协议 传输之前不需要键 ...

  6. 24 使用Maven 或 Gradle构建groovy

    1       使用Maven 或 Gradle构建groovy 1.1  使用maven构建groovy pom.xml file. <dependencies>     ... oth ...

  7. pycharm 虚拟环境virtualenv迁移到别的机器 无法读取包的问题

    将virtualenv迁移到别的机器时,发现pycharm 总是无法读取目录下所在的包,后来经过实验终于找到了问题所在: 将自己所建的虚拟环境目录下的orig-prefix.txt中保存的路径,改成新 ...

  8. 542 01 Matrix 01 矩阵

    给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离.两个相邻元素间的距离为 1 .示例 1:输入:0 0 00 1 00 0 0输出:0 0 00 1 00 0 0 示例 2:输入: ...

  9. 181. 将整数A转换为B

    181. 将整数A转换为B 如果要将整数A转换为B,需要改变多少个bit位? 注意事项 Both n and m are 32-bit integers. 您在真实的面试中是否遇到过这个题? Yes ...

  10. JavaScript alert()函数

    avaScript alert()函数 alert--弹出消息对话框(对话框中有一个OK按钮) alert,中文"提醒"的意思 alert函数语法 alert(str); aler ...