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]作为右下节点的时候的最大矩形的边长,JAVA实现如下:

    public int maximalSquare(char[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0)
return 0;
int res = 0;
int[] dp = new int[matrix[0].length];
for (int i = 0; i < matrix[0].length; i++)
if (matrix[0][i] == '1'){
dp[i] = 1;
res=1;
}
for (int i = 1; i < matrix.length; i++) {
dp[0] = matrix[i][0] == '1' ? 1 : 0;
res=Math.max(res, dp[0]);
for (int j = 1; j < matrix[0].length; j++) {
if(matrix[i][j]=='0')
dp[j]=0;
else if (dp[j] == dp[j - 1]
&& matrix[i - dp[j]][j - dp[j]] == '0') {
dp[j] = Math.min(dp[j], dp[j - 1]);
} else
dp[j] = Math.min(dp[j], dp[j - 1]) + 1;
res=Math.max(res, dp[j]);
}
}
return res*res;
}

Java for LeetCode 221 Maximal Square的更多相关文章

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

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

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

  3. (medium)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. [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming

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

  5. Leetcode 221. Maximal Square

    本题用brute force超时.可以用DP,也可以不用. dp[i][j] 代表 以(i,j)为右下角正方形的边长. class Solution(object): def maximalSquar ...

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

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

  7. 【LeetCode】221. Maximal Square

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

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

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

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

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

随机推荐

  1. [CentOs]ip操作

    摘要 在虚机里面安装好centos之后,需要知道centos的ip,方便以后连接时使用. 查看ip命令 命令 ifconfig 能查看到信息,说明已经配置过了,如果没配置过,可以通过下面的方式进行配置 ...

  2. VS Code 开发asp.net core 遇到的坑

    摘要 微软发布.NET Core 1.0,ASP.NET Core 1.0 与 Entity Framewok 1.0也有一段时间了,一直没进行这方面的学习,节前公司让调研这方面的可行性.所以还是从最 ...

  3. 解决redhat的未注册问题

    昨天安装第五步的时候:开始是没有网,,,居然ping不通  网  ,服务器也ping不通,,,,,可能和我前几天删除了网络适配器有关,,把linux桥接对应的适配器给删了,,, 解决办法是打开虚拟网络 ...

  4. 终端下使用cocopods

    http://blog.csdn.net/showhilllee/article/details/38398119

  5. ACM3 求最值

    /*2*2014.11.18*求最值*描述:给定N个整数(1<=N<=100),求出这N个数中的最大值,最小值.*输入:多组数据,第一行为一个整数N,第二行为N个不超过100的正整数,用空 ...

  6. [转]C++模板学习

    1. 模板的概念. 我们已经学过重载(Overloading),对重载函数而言,C++的检查机制能通过函数参数的不同及所属类的不同.正确的调用重载函数.例如,为求两个数的最大值,我们定义MAX()函数 ...

  7. javacomm64位用不了,可以使用RXTXcomm for x64

    安装完后把导入包名改一下就行了! 附上读串口代码: /* * @(#)SimpleRead.java 1.12 98/06/25 SMI * * Copyright (c) 1998 Sun Micr ...

  8. 详解 iOS 上机题!附个人见解

    庸者的救赎2015-11-20 02:30:23 AFN那个使用的时候不需要弱引用的,因为从你的封装方式来看,那个block并不会被当前视图控制器持有,而是被manager持有了,所以不需要__wea ...

  9. select根据text选择项与select其它操作

    // 6.设置select中text="paraText"的第一个Item为选中 function jsSelectItemByValue(objSelect, objItemTe ...

  10. CSS3实现背景颜色渐变 摘抄

    一. Webkit浏览器 (1) 第一种写法: background:-webkit-gradient(linear ,10% 10%,100% 100%, color-stop(0.14,rgb(2 ...