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

Example:

Input: 

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0 Output: 4
class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length == 0) {
return 0;
} int row = matrix.length;
int col = matrix[0].length;
int[][] sqr = new int[row + 1][col + 1];
int res = 0; for (int i = 1; i <= row; i++) {
for(int j = 1; j <= col; j++) {
if (matrix[i - 1][j - 1] == '1') {
sqr[i][j] = Math.min(Math.min(sqr[i - 1][j], sqr[i][j - 1]), sqr[i - 1][j - 1]) + 1;
res = Math.max(res, sqr[i][j]);
}
}
}
return res * res;
}
}

[LC] 221. Maximal Square的更多相关文章

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

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

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

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

  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

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

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

  6. Java for 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. 221. Maximal Square -- 矩阵中1组成的最大正方形

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

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

  9. 221. Maximal Square

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

随机推荐

  1. 程序Dog的大梦想

    一.我是程序狗 "怎么又是任宏啊,每天都起这么早,要命啊--" "人家任宏可是要成为学霸的男人,咱们这些凡夫俗子啊,理解不了这么伟大的理想--"----微电影& ...

  2. 洛谷 P1731 [NOI1999]生日蛋糕 && POJ 1190 生日蛋糕

    题目传送门(洛谷)  OR 题目传送门(POJ) 解题思路: 一道搜索题,暴力思路比较容易想出来,但是这道题不剪枝肯定会TLE.所以这道题难点在于如何剪枝. 1.如果当前状态答案已经比我们以前某个状态 ...

  3. [FJOI2015]火星商店问题(线段树分治+可持久化Trie)

    重新写一年前抄题解的那题,当时我啥都不会只是Ctrl+C,Ctrl+V写过的题,今天重新写一遍. 题解: 不会线段树分治,还是学一下这东西吧,这是我的第一道线段树分治. 首先对于特殊商品,可以直接可持 ...

  4. Spring Cloud Alibaba 教程 | Nacos(五)

    扩展配置(extended configurations) 通过之前的学习,我们知道应用引入nacos配置中心之后默认将会加载Data ID= ${prefix} - ${spring.profile ...

  5. Python笔记_第一篇_面向过程第一部分_6.循环控制语句(while 和 for)_

    承接条件控制语句.条件控制语句像大树一样有很多的之差,那条路径通(也就是表达式判断为True)就会往哪一个树杈去运行,万涓溪水汇成大河.对于常用的程序结构形式,还有一种这篇文章就会讲解,那就是循环控制 ...

  6. 查看opencv-python编译信息

    python -c "import cv2; print(cv2.getBuildInformation())" General configuration for OpenCV ...

  7. 模仿u-boot的makefile结构

    u-boot(2014.04)是通过顶层makefile调用各子目录中的makefile来实现整个工程的编译的,实际上子目录的makefile是include进来的.这里仿照这种结构写个模板测试一下. ...

  8. [原]C++新标准之std::thread

    原 总结 C++11 thread  概览 std::thread 类定义 各个成员函数的简单介绍 例子 更多 参考资料 概览 从C++11开始提供了线程的支持,终于可以方便的编写跨平台的线程代码了. ...

  9. [Sdoi2013]森林(启发式合并+主席树)

    对于操作1,显然可以使用主席树维护,然后对于一条链(x,y),假设lca为f,根为rt,则(rt,x)+(rt,y)-(rt,f)-(rt,fa[f])即为所求的链,在主席树上直接查询即可,查询方式类 ...

  10. Softether使用本地网桥

    https://maytalkhao.com/archives/826 以下步骤都是按照上面这篇文章来的,具体细节如下 一.使用Softether  VPN Server Manager软件添加本地网 ...