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. Element.shadowRoot

    Element.shadowRoot http://www.zhuyuntao.cn/shadow-dom的样式/ Shadow DOM的样式 我们已经可以使用原生的操作DOM的方式和使用模板的方式来 ...

  2. 内存管理之栈stack

    1.什么是栈   栈是一种数据结构,C语言中使用栈来保存局部变量.栈是被发明出来管理内存的.2.栈管理内存的特点(小内存.自动化)   先进后出  FILO   first in last out   ...

  3. tar.xz文件

    创建或解压tar.xz文件的方法 习惯了 tar czvf 或 tar xzvf 的人可能碰到 tar.xz也会想用单一命令搞定解压或压缩.其实不行 tar里面没有征对xz格式的参数比如 z是针对 g ...

  4. Python列表中去重的多种方法

    怎么快速的对列表进行去重呢,去重之后原来的顺序会不会改变呢? 去重之后顺序会改变 set去重 列表去重改变原列表的顺序了 l1 = [1,4,4,2,3,4,5,6,1] l2 = list(set( ...

  5. JavaScript sort()方法总结

    sort() 方法用于对数组的元素进行排序. 语法:arrayObject.sort(sortby):参数sortby可选.规定排序顺序.必须是函数. 注:如果调用该方法时没有使用参数,将按字母顺序对 ...

  6. pearson相关系数的介绍

  7. [LC] 51. N-Queens

    Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a d ...

  8. 将iso mount 到nfs 目录问题

    最近有个需求,需要在多台系统安装程序,安装文件是iso 格式的,最普通的办法就是拷贝到其它系统,然后mount loop 到本地目录. 但是比较麻烦,而且当前已经有一个nfs 服务端了,于是想出一个办 ...

  9. Xpath编码问题解决

    使用Xpath获取属性时,出现乱码问题,解决办法找了好多,终于解决,特将办法贴在这,供大家尝试 不要直接简单的将爬取的网页设置为utf-8, 先通过print(r.encoding)输出看看爬取的是什 ...

  10. 面向对象 part5

    构造函数模式与原型模式结合 function Person(name) = { this.name = name this.friends = ["a", "b" ...