Skip to content

Commit c654d01

Browse files
refactor 531
1 parent efe62ea commit c654d01

File tree

1 file changed

+28
-26
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+28
-26
lines changed
Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 531. Lonely Pixel I
5+
*
46
* 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.
99
1010
Example:
1111
Input:
@@ -15,42 +15,44 @@
1515
1616
Output: 3
1717
Explanation: All the three 'B's are black lonely pixels.
18+
1819
Note:
1920
The range of width and height of the input 2D array is [1,500].
2021
*/
2122
public class _531 {
2223

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+
}
3134
}
3235
}
36+
return count;
3337
}
34-
return count;
35-
}
3638

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+
}
4245
}
4346
}
44-
}
4547

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+
}
5053
}
5154
}
55+
return true;
5256
}
53-
return true;
5457
}
55-
5658
}

0 commit comments

Comments
 (0)