|
1 | 1 | package com.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 | public class _531 {
|
22 | 23 |
|
23 |
| - public int findLonelyPixel(char[][] picture) { |
24 |
| - int m = picture.length; |
25 |
| - int n = picture[0].length; |
26 |
| - int count = 0; |
27 |
| - for (int i = 0; i < m; i++) { |
28 |
| - for (int j = 0; j < n; j++) { |
29 |
| - if (picture[i][j] == 'B' && isLonely(i, j, picture, m, n)) { |
30 |
| - count++; |
| 24 | + public static class Solution1 { |
| 25 | + public int findLonelyPixel(char[][] picture) { |
| 26 | + int m = picture.length; |
| 27 | + int n = picture[0].length; |
| 28 | + int count = 0; |
| 29 | + for (int i = 0; i < m; i++) { |
| 30 | + for (int j = 0; j < n; j++) { |
| 31 | + if (picture[i][j] == 'B' && isLonely(i, j, picture, m, n)) { |
| 32 | + count++; |
| 33 | + } |
31 | 34 | }
|
32 | 35 | }
|
| 36 | + return count; |
33 | 37 | }
|
34 |
| - return count; |
35 |
| - } |
36 | 38 |
|
37 |
| - private boolean isLonely(int row, int col, char[][] picture, int m, int n) { |
38 |
| - for (int i = 0; i < m; i++) { |
39 |
| - if (i != row) { |
40 |
| - if (picture[i][col] == 'B') { |
41 |
| - return false; |
| 39 | + private boolean isLonely(int row, int col, char[][] picture, int m, int n) { |
| 40 | + for (int i = 0; i < m; i++) { |
| 41 | + if (i != row) { |
| 42 | + if (picture[i][col] == 'B') { |
| 43 | + return false; |
| 44 | + } |
42 | 45 | }
|
43 | 46 | }
|
44 |
| - } |
45 | 47 |
|
46 |
| - for (int j = 0; j < n; j++) { |
47 |
| - if (j != col) { |
48 |
| - if (picture[row][j] == 'B') { |
49 |
| - return false; |
| 48 | + for (int j = 0; j < n; j++) { |
| 49 | + if (j != col) { |
| 50 | + if (picture[row][j] == 'B') { |
| 51 | + return false; |
| 52 | + } |
50 | 53 | }
|
51 | 54 | }
|
| 55 | + return true; |
52 | 56 | }
|
53 |
| - return true; |
54 | 57 | }
|
55 |
| - |
56 | 58 | }
|
0 commit comments