-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
32 lines (32 loc) · 977 Bytes
/
Solution.java
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
32
public class Solution {
public int maximalSquare(char[][] matrix) {
int cross=matrix.length;
if (cross==0) {
return 0;
}
int vertical=matrix[0].length;
int max=0;
int[][] size=new int[cross][vertical];
for (int i = 0; i < cross; i++) {
size[i][0]=matrix[i][0]=='1'?1:0;
if (size[i][0]>max) {
max=size[i][0];
}
}
for (int i = 0; i < vertical; i++) {
size[0][i]=matrix[0][i]=='1'?1:0;
if (size[0][i]>max) {
max=size[0][i];
}
}
for (int i = 1; i < cross; i++) {
for (int j = 1; j < vertical; j++) {
size[i][j]=matrix[i][j]=='1'?Math.min(Math.min(size[i-1][j], size[i][j-1]), size[i-1][j-1])+1:0;
if (size[i][j]>max) {
max=size[i][j];
}
}
}
return max*max;
}
}