|
1 | 1 | packagecom.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | /**
|
| 4 | + * 531. Lonely Pixel I |
| 5 | + * |
4 | 6 | * Given a picture consisting of black and white pixels, find the number of black lonely pixels.
|
5 |
| -
|
6 |
| - The picture is represented by a 2D char array consisting of 'B' and 'W', which means black and white pixels respectively. |
7 |
| -
|
8 |
| - A black lonely pixel is character 'B' that located at a specific position where the same row and same column don't have any other black pixels. |
| 7 | + * The picture is represented by a 2D char array consisting of 'B' and 'W', which means black and white pixels respectively. |
| 8 | + * A black lonely pixel is character 'B' that located at a specific position where the same row and same column don't have any other black pixels. |
9 | 9 |
|
10 | 10 | Example:
|
11 | 11 | Input:
|
|
15 | 15 |
|
16 | 16 | Output: 3
|
17 | 17 | Explanation: All the three 'B's are black lonely pixels.
|
| 18 | +
|
18 | 19 | Note:
|
19 | 20 | The range of width and height of the input 2D array is [1,500].
|
20 | 21 | */
|
21 | 22 | publicclass_531 {
|
22 | 23 |
|
23 |
| -publicintfindLonelyPixel(char[][]picture) { |
24 |
| -intm =picture.length; |
25 |
| -intn =picture[0].length; |
26 |
| -intcount =0; |
27 |
| -for (inti =0;i <m;i++) { |
28 |
| -for (intj =0;j <n;j++) { |
29 |
| -if (picture[i][j] =='B' &&isLonely(i,j,picture,m,n)) { |
30 |
| -count++; |
| 24 | +publicstaticclassSolution1 { |
| 25 | +publicintfindLonelyPixel(char[][]picture) { |
| 26 | +intm =picture.length; |
| 27 | +intn =picture[0].length; |
| 28 | +intcount =0; |
| 29 | +for (inti =0;i <m;i++) { |
| 30 | +for (intj =0;j <n;j++) { |
| 31 | +if (picture[i][j] =='B' &&isLonely(i,j,picture,m,n)) { |
| 32 | +count++; |
| 33 | + } |
31 | 34 | }
|
32 | 35 | }
|
| 36 | +returncount; |
33 | 37 | }
|
34 |
| -returncount; |
35 |
| - } |
36 | 38 |
|
37 |
| -privatebooleanisLonely(introw,intcol,char[][]picture,intm,intn) { |
38 |
| -for (inti =0;i <m;i++) { |
39 |
| -if (i !=row) { |
40 |
| -if (picture[i][col] =='B') { |
41 |
| -returnfalse; |
| 39 | +privatebooleanisLonely(introw,intcol,char[][]picture,intm,intn) { |
| 40 | +for (inti =0;i <m;i++) { |
| 41 | +if (i !=row) { |
| 42 | +if (picture[i][col] =='B') { |
| 43 | +returnfalse; |
| 44 | + } |
42 | 45 | }
|
43 | 46 | }
|
44 |
| - } |
45 | 47 |
|
46 |
| -for (intj =0;j <n;j++) { |
47 |
| -if (j !=col) { |
48 |
| -if (picture[row][j] =='B') { |
49 |
| -returnfalse; |
| 48 | +for (intj =0;j <n;j++) { |
| 49 | +if (j !=col) { |
| 50 | +if (picture[row][j] =='B') { |
| 51 | +returnfalse; |
| 52 | + } |
50 | 53 | }
|
51 | 54 | }
|
| 55 | +returntrue; |
52 | 56 | }
|
53 |
| -returntrue; |
54 | 57 | }
|
55 |
| - |
56 | 58 | }
|