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

Commitd159d7d

Browse files
refactor 533
1 parent0d19238 commitd159d7d

File tree

2 files changed

+47
-48
lines changed

2 files changed

+47
-48
lines changed

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

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -38,53 +38,55 @@
3838
The range of width and height of the input 2D array is [1,200].
3939
*/
4040
publicclass_533 {
41-
/**Credit: https://discuss.leetcode.com/topic/81686/verbose-java-o-m-n-solution-hashmap/5
42-
*
43-
* This program is very well designed to do things:
44-
* 1. it scans the entire matrix once, but does two things in this scan:
45-
* first it records an array of cols that keeps count of how many 'B' are there in each column;
46-
* second, at the end of traversing each column, it puts this entire row as the key into a HashMap
47-
* when there N number of 'B's in this row.
48-
*
49-
* 2. then we could go through the HashMap keyset:
50-
* if one row has N number of 'B's, we go through this row's each column to see if any element in this row is 'B' and also that element's column has N 'B's*/
51-
publicintfindBlackPixel(char[][]picture,intN) {
52-
if (picture ==null ||picture.length ==0 ||picture[0].length ==0) {
53-
return0;
54-
}
55-
intm =picture.length;
56-
intn =picture[0].length;
57-
int[]cols =newint[n];
58-
Map<String,Integer>map =newHashMap<>();
59-
StringBuilderstringBuilder =newStringBuilder();
60-
for (inti =0;i <m;i++) {
61-
intcount =0;
62-
for (intj =0;j <n;j++) {
63-
if (picture[i][j] =='B') {
64-
count++;
65-
cols[j]++;
66-
}
67-
stringBuilder.append(picture[i][j]);
41+
publicstaticclassSolution1 {
42+
/**
43+
* Credit: https://discuss.leetcode.com/topic/81686/verbose-java-o-m-n-solution-hashmap/5
44+
* This program is very well designed to do things:
45+
* 1. it scans the entire matrix once, but does two things in this scan:
46+
* first it records an array of cols that keeps count of how many 'B' are there in each column;
47+
* second, at the end of traversing each column, it puts this entire row as the key into a HashMap
48+
* when there N number of 'B's in this row.
49+
* 2. then we could go through the HashMap keyset:
50+
* if one row has N number of 'B's, we go through this row's each column to see if any element in this row is 'B' and also that element's column has N 'B's
51+
*/
52+
publicintfindBlackPixel(char[][]picture,intN) {
53+
if (picture ==null ||picture.length ==0 ||picture[0].length ==0) {
54+
return0;
6855
}
69-
if (count ==N) {
70-
/**we use this entire row string as key for the map*/
71-
map.put(stringBuilder.toString(),map.getOrDefault(stringBuilder.toString(),0) +1);
56+
intm =picture.length;
57+
intn =picture[0].length;
58+
int[]cols =newint[n];
59+
Map<String,Integer>map =newHashMap<>();
60+
StringBuilderstringBuilder =newStringBuilder();
61+
for (inti =0;i <m;i++) {
62+
intcount =0;
63+
for (intj =0;j <n;j++) {
64+
if (picture[i][j] =='B') {
65+
count++;
66+
cols[j]++;
67+
}
68+
stringBuilder.append(picture[i][j]);
69+
}
70+
if (count ==N) {
71+
/**we use this entire row string as key for the map*/
72+
map.put(stringBuilder.toString(),map.getOrDefault(stringBuilder.toString(),0) +1);
73+
}
74+
stringBuilder.setLength(0);
7275
}
73-
stringBuilder.setLength(0);
74-
}
7576

76-
intanswer =0;
77-
for (Stringkey :map.keySet()) {
78-
if (map.get(key) !=N) {
79-
continue;
80-
}
81-
for (inti =0;i <n;i++) {
82-
if (key.charAt(i) =='B' &&cols[i] ==N) {
83-
answer +=N;
77+
intanswer =0;
78+
for (Stringkey :map.keySet()) {
79+
if (map.get(key) !=N) {
80+
continue;
81+
}
82+
for (inti =0;i <n;i++) {
83+
if (key.charAt(i) =='B' &&cols[i] ==N) {
84+
answer +=N;
85+
}
8486
}
8587
}
88+
returnanswer;
8689
}
87-
returnanswer;
8890
}
8991

9092
}

‎src/test/java/com/fishercoder/_533Test.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66

77
importstaticorg.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 5/25/17.
11-
*/
129
publicclass_533Test {
13-
privatestatic_533test;
10+
privatestatic_533.Solution1solution1;
1411
privatestaticchar[][]picture;
1512

1613
@BeforeClass
1714
publicstaticvoidsetup() {
18-
test =new_533();
15+
solution1 =new_533.Solution1();
1916
}
2017

2118
@Test
@@ -25,12 +22,12 @@ public void test1() {
2522
{'W','B','W','B','B','W'},
2623
{'W','B','W','B','B','W'},
2724
{'W','W','B','W','B','W'}};
28-
assertEquals(6,test.findBlackPixel(picture,3));
25+
assertEquals(6,solution1.findBlackPixel(picture,3));
2926
}
3027

3128
@Test
3229
publicvoidtest2() {
3330
picture =newchar[][]{{'B'}};
34-
assertEquals(1,test.findBlackPixel(picture,1));
31+
assertEquals(1,solution1.findBlackPixel(picture,1));
3532
}
3633
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp