File tree Expand file tree Collapse file tree 1 file changed +8
-16
lines changed Expand file tree Collapse file tree 1 file changed +8
-16
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int maximalSquare (char [][] matrix ) {
3
- if (matrix == null || matrix .length == 0 ) {
4
- return 0 ;
5
- }
3
+ int sideLen = 0 ;
6
4
7
- int result = 0 ;
8
- int [][] dp = new int [matrix .length ][matrix [0 ].length ];
5
+ int [][] dp = new int [matrix .length + 1 ][matrix [0 ].length + 1 ];
9
6
10
- for (int i = 0 ; i < matrix .length ; i ++) {
11
- for (int j = 0 ; j < matrix [i ].length ; j ++) {
12
- if (i == 0 || j == 0 ) {
13
- dp [i ][j ] = matrix [i ][j ] - '0' ;
14
- } else {
15
- if (matrix [i ][j ] == '1' ) {
16
- dp [i ][j ] = 1 + Math .min (dp [i - 1 ][j - 1 ], Math .min (dp [i - 1 ][j ], dp [i ][j - 1 ]));
17
- }
7
+ for (int i = 1 ; i <= matrix .length ; i ++) {
8
+ for (int j = 1 ; j <= matrix [0 ].length ; j ++) {
9
+ if (matrix [i - 1 ][j - 1 ] == '1' ) {
10
+ dp [i ][j ] = Math .min (dp [i - 1 ][j - 1 ], Math .min (dp [i - 1 ][j ], dp [i ][j - 1 ])) + 1 ;
11
+ sideLen = Math .max (sideLen , dp [i ][j ]);
18
12
}
19
-
20
- result = Math .max (result , dp [i ][j ]);
21
13
}
22
14
}
23
15
24
- return result * result ;
16
+ return sideLen * sideLen ;
25
17
}
26
18
}
You can’t perform that action at this time.
0 commit comments