Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcbd3a81

Browse files
refactor 221
1 parent80a79f6 commitcbd3a81

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

‎src/main/java/com/fishercoder/solutions/_221.java

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,33 @@
1515
*/
1616
publicclass_221 {
1717

18-
/**The idea is pretty straightforward: use a 2d dp table to store the intermediate results*/
19-
publicstaticintmaximalSquare(char[][]matrix) {
20-
if (matrix ==null ||matrix.length ==0) {
21-
return0;
22-
}
23-
intm =matrix.length;
24-
intn =matrix[0].length;
25-
intmax =Integer.MIN_VALUE;
26-
int[][]dp =newint[m][n];
27-
for (inti =0;i <m;i++) {
28-
for (intj =0;j <n;j++) {
29-
if (i ==0 ||j ==0) {
30-
dp[i][j] =(matrix[i][j] =='1') ?1 :0;
31-
}else {
32-
if (matrix[i][j] =='0') {
33-
dp[i][j] =0;
18+
publicstaticclassSolution1 {
19+
/**
20+
* The idea is pretty straightforward: use a 2d dp table to store the intermediate results
21+
*/
22+
publicintmaximalSquare(char[][]matrix) {
23+
if (matrix ==null ||matrix.length ==0) {
24+
return0;
25+
}
26+
intm =matrix.length;
27+
intn =matrix[0].length;
28+
intmax =Integer.MIN_VALUE;
29+
int[][]dp =newint[m][n];
30+
for (inti =0;i <m;i++) {
31+
for (intj =0;j <n;j++) {
32+
if (i ==0 ||j ==0) {
33+
dp[i][j] =(matrix[i][j] =='1') ?1 :0;
3434
}else {
35-
dp[i][j] =Math.min(dp[i -1][j],Math.min(dp[i][j -1],dp[i -1][j -1])) +1;
35+
if (matrix[i][j] =='0') {
36+
dp[i][j] =0;
37+
}else {
38+
dp[i][j] =Math.min(dp[i -1][j],Math.min(dp[i][j -1],dp[i -1][j -1])) +1;
39+
}
3640
}
41+
max = (max <dp[i][j]) ?dp[i][j] :max;
3742
}
38-
max = (max <dp[i][j]) ?dp[i][j] :max;
3943
}
44+
returnmax *max;
4045
}
41-
returnmax *max;
42-
}
43-
44-
publicstaticvoidmain(String...strings) {
45-
char[][]matrix =newchar[][]{
46-
{'1','0','1','0','0'},
47-
{'1','0','1','1','1'},
48-
{'1','1','1','1','1'},
49-
{'1','0','0','1','0'},
50-
};
51-
System.out.println(maximalSquare(matrix));
5246
}
5347
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._221;
4+
importcom.fishercoder.solutions._50;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticorg.junit.Assert.assertEquals;
9+
10+
publicclass_221Test {
11+
privatestatic_221.Solution1solution1;
12+
privatestaticchar[][]matrix;
13+
14+
@BeforeClass
15+
publicstaticvoidsetup() {
16+
solution1 =new_221.Solution1();
17+
}
18+
19+
@Test
20+
publicvoidtest1() {
21+
matrix =newchar[][]{
22+
{'1','0','1','0','0'},
23+
{'1','0','1','1','1'},
24+
{'1','1','1','1','1'},
25+
{'1','0','0','1','0'},
26+
};
27+
28+
assertEquals(4,solution1.maximalSquare(matrix));
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp