-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0221.java
More file actions
30 lines (27 loc) · 1.16 KB
/
Copy pathLeetCode0221.java
File metadata and controls
30 lines (27 loc) · 1.16 KB
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
/* Maximal Square
* Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
* Output: 4
* */
public class LeetCode0221 {
public static void main(String args[]) {
char[][] matrix = {{'1', '0', '1', '0', '0',}, {'1', '0', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '0', '0', '1', '0'}};
System.out.println(maximalSquare(matrix));
}
public static int maximalSquare(char[][] matrix) {
int row = matrix.length;
int column = matrix[0].length;
if (matrix == null || row == 0 || column == 0)
return 0;
int[][] dp = new int[row + 1][column + 1]; //创建矩阵,增加第一行和第一列为了防止溢出
int maxSquare = 0;
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= column; j++) {
if (matrix[i - 1][j - 1] == '1') {
dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
maxSquare = Math.max(maxSquare, dp[i][j]);
}
}
}
return maxSquare * maxSquare;
}
}