-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution221.java
More file actions
31 lines (29 loc) · 931 Bytes
/
Solution221.java
File metadata and controls
31 lines (29 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 最大正方形
* @date: 2019/02/21
*/
public class Solution221 {
public int maximalSquare(char[][] matrix) {
if (null == matrix || 0 >= matrix.length) {
return 0;
}
int m = matrix.length, n = matrix[0].length;
// dp[i][j]表示:以第(i,j)个元素为正方形右下角元素时,最大的边长.
int[][] dp = new int[m + 1][n + 1];
int ans = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
// 如果当前位置为1
if ('1' == matrix[i - 1][j - 1]) {
dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j - 1]), dp[i - 1][j]) + 1;
if (dp[i][j] > ans) {
ans = dp[i][j];
}
}
}
}
return ans * ans;
}
}