Question:
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 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 0Return 4.
Solution:
b的最左列和最上列没有意义,b[i][j]记录的是自己作为方形右下角的情况。
public class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix.length == 0) return 0;
int m=matrix.length;
int n=matrix[0].length;
int[][] b=new int[m+1][n+1];
int result=0;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(matrix[i-1][j-1]=='1')
{
b[i][j]=Math.min(b[i-1][j],Math.min(b[i][j-1],b[i-1][j-1]))+1;
result=result>b[i][j]?result:b[i][j];
}
}
}
return result*result;
}
}
看到一个不错的思路讲解,就是太复杂了。。。明明可以把数组扩大一行一列解决问题http://www.07net01.com/2015/09/920587.html