题解 | #最大正方形#
最大正方形
http://www.nowcoder.com/practice/0058c4092cec44c2975e38223f10470e
public class Solution {
/**
* 最大正方形
* @param matrix char字符型二维数组
* @return int整型
*/
public int solve (char[][] matrix) {
// write code here
if(matrix == null || matrix.length == 0){
return 0;
}
//dp[m][n],其中dp[i][j]表示的是在矩阵中以坐标(i,j)为右下角的最大正方形边长
int height = matrix.length;
int width = matrix[0].length;
int[][] dp = new int[height][width];
int maxSize = 0;
for(int i = 0;i< height;i++){
for(int j = 0;j<width;j++){
if(i == 0||j == 0){
if(matrix[i][j] == '1'){
dp[i][j] = 1;
}
continue;
}
if(matrix[i][j] == '1'){
dp[i][j] = Math.min(dp[i-1][j], Math.min(dp[i-1][j-1], dp[i][j-1])) + 1;
maxSize = Math.max(maxSize, dp[i][j]);
}
}
}
return maxSize * maxSize;
}
}