class Solution {
public int maximalSquare(char[][] matrix) {
int m = matrix.length, n = matrix[0].length;
int[][] dp = new int[m][n];
if (matrix[0][0] == '1') {
dp[0][0] = 1;
}
for (int i = 1; i < m; i++) {
if (matrix[i][0] == '1') {
dp[i][0] = 1;
}
}
for (int i = 1; i < n; i++) {
if (matrix[0][i] == '1') {
dp[0][i] = 1;
}
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (matrix[i][j] == '1') {
dp[i][j] = Math.min(
Math.min(dp[i - 1][j], dp[i][j - 1]),
dp[i - 1][j - 1]
) + 1;
}
}
}
int max = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (dp[i][j] > max) {
max = dp[i][j];
}
}
}
return max * max;
}
}
/*
0 0 1 1
0 1 1 1
0 1 1 1
0 1 1 1
*/
08-06
260
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-09
383
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-10
145
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交