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.

dp问题,用一个dp[i][j]保存matrix[i][j]作为右下节点的时候的最大矩形的边长

if (matrix[i][j] == '0') dp[i][j] = 0;

else dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1;

 1 public class Solution {
 2     public int maximalSquare(char[][] matrix) {
 3         int res = 0;
 4         if (matrix==null || matrix.length==0 || matrix[0].length==0) return res;
 5         int[][] dp = new int[matrix.length][matrix[0].length];
 6         int maxEdge = 0;
 7         for (int i=0; i<matrix.length; i++) {
 8             if (matrix[i][0] == '1') dp[i][0] = 1;
 9             else dp[i][0] = 0;
10             maxEdge = Math.max(maxEdge, dp[i][0]);
11         }
12         for (int j=1; j<matrix[0].length; j++) {
13             if (matrix[0][j] == '1') dp[0][j] = 1;
14             else dp[0][j] = 0;
15             maxEdge = Math.max(maxEdge, dp[0][j]);
16         }
17         for (int i=1; i<matrix.length; i++) {
18             for (int j=1; j<matrix[0].length; j++) {
19                 if (matrix[i][j] == '0') {
20                     dp[i][j] = 0;
21                     continue;
22                 }
23                 dp[i][j] = Math.min(dp[i-1][j-1], Math.min(dp[i-1][j], dp[i][j-1])) + 1;
24                 maxEdge = Math.max(maxEdge, dp[i][j]);
25             }
26         }
27         return maxEdge * maxEdge;
28     }
29 }

Leetcode: Maximal Square相关推荐

  1. LeetCode Maximal Square(最大子矩阵)

    问题:给出一个由0,1组成的二维数组,求由1组成的最大子矩阵 思路:第一种方式使用暴力法,在遍历二维数组时,如果当前元素是1,则以当前位置为起点,判断先增的一行,一列是否全是1,如果是,则将当前边长度 ...

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

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

  3. leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题

    Largest Rectangle问题 题目描述 Given n non-negative integers representing the histogram's bar height where ...

  4. LeetCode Top 100 Liked Questions 221. Maximal Square (Java版; Medium)

    welcome to my blog LeetCode Top 100 Liked Questions 221. Maximal Square (Java版; Medium) 题目描述 Given a ...

  5. 经典算法题之Maximal Square

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 作者:叶    虎 Maximal Square是道非常有意思的算 ...

  6. leetcode 221. Maximal Square | 221. 最大正方形(优化的暴力解法+动态规划解法)

    题目 https://leetcode.com/problems/maximal-square/ 题解 方法1:最暴力解 O((m*n)^2) public class Solution {publi ...

  7. LeetCode 221. Maximal Square (最大正方形)

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

  8. 【LeetCode从零单排】No221.Maximal Square

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

  9. [leetcode ]221. Maximal Square c语言

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  10. [Swift]LeetCode221. 最大正方形 | Maximal Square

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

最新文章

  1. win10 rabbitMQ的安装与测试
  2. CORS跨域实现思路及相关解决方案
  3. mysql events_mysql定时器Events
  4. php gd库画线,[PHP] GD库(十)绘制线段与圆弧 imageline、imagesetstyle 与 imagearc 函数...
  5. c4droid语言字体颜色,关于printf如何输出颜色
  6. 批量修改栏目名_必收APP-效率高不含糊,批量管理功能上线了!
  7. gitlab 修改git clone地址为指定域名
  8. android 集成同一interface不同泛型_【Java视频教程】day30-泛型??
  9. linux 文件系统路径,Linux编程 1 (文件系统路径说明, 目录结构说明)
  10. RocketMQ的各种集群模式的搭建和消息可靠性保证和服务可用性描述
  11. 2021柳州市地区高考成绩排名查询,2021年柳州所有高中排名一览表
  12. matlab中角度,利用 matlab 计算各种角度
  13. 高中数学:抛物线专题讲解利用韦达定理求解技巧
  14. IDEA全局替换快捷键
  15. Vue.js实现文章评论和回复评论功能
  16. MATLAB读取10bit的raw格式图片代码
  17. 关于小程序 scroll
  18. JavaScript数组内置方法-知识
  19. 示波器表笔旁边的夹子是什么_示波器探头容易忽略的几个问题
  20. [CTF]中那些脑洞大开的编码和加密

热门文章

  1. PHP-----strpos() 函数的用法
  2. python并发编程方法_一文了解Python并发编程的工程实现方法
  3. excel使用教程_办公软件excel表格制作教程
  4. matlab设置ylabel,关于ylabel设置的问题
  5. php 中tp3.2中c,thinkphp3.2笔记(2)调试模式,配置项C,创建模块, 四种URL模式,URL生成,跳转...
  6. java 不安全操作_Java新手求助:怎么会出现使用了未经检查或不安全的操作。
  7. python-gui-pyqt5的使用方法-4--自定义信号的初识--多参数的使用
  8. 单元格赋值与联动 例:C1值赋予D1 ,并将D1的值传给图表元素联动
  9. deeplearning.ai——构建循环神经网络
  10. 实现关于跨二级域名和1.1和2.0版.net Forms身份验证体制的问题和解决办法.