
Input: picture =[["W","W","B"],["W","B","W"],["B","W","W"]]Output: 3Explanation: All the three 'B's are black lonely pixels.
A black pixel is lonely if it is the only ‘B’ in its row and column. By counting the number of ‘B’s in each row and column, we can efficiently check for lonely pixels.
classSolution {
public:int findLonelyPixel(vector<vector<char>>& pic) {
int m = pic.size(), n = pic[0].size();
vector<int> row(m), col(n);
for (int i =0; i < m; ++i)
for (int j =0; j < n; ++j)
if (pic[i][j] =='B') row[i]++, col[j]++;
int ans =0;
for (int i =0; i < m; ++i)
for (int j =0; j < n; ++j)
if (pic[i][j] =='B'&& row[i] ==1&& col[j] ==1) ans++;
return ans;
}
};
funcfindLonelyPixel(pic [][]byte) int {
m, n:= len(pic), len(pic[0])
row:= make([]int, m)
col:= make([]int, n)
fori:=0; i < m; i++ {
forj:=0; j < n; j++ {
ifpic[i][j] =='B' {
row[i]++col[j]++ }
}
}
ans:=0fori:=0; i < m; i++ {
forj:=0; j < n; j++ {
ifpic[i][j] =='B'&&row[i] ==1&&col[j] ==1 {
ans++ }
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
publicintfindLonelyPixel(char[][] pic) {
int m = pic.length, n = pic[0].length;
int[] row =newint[m], col =newint[n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (pic[i][j]=='B') { row[i]++; col[j]++; }
int ans = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (pic[i][j]=='B'&& row[i]== 1 && col[j]== 1) ans++;
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funfindLonelyPixel(pic: Array<CharArray>): Int {
val m = pic.size
val n = pic[0].size
val row = IntArray(m)
val col = IntArray(n)
for (i in0 until m)
for (j in0 until n)
if (pic[i][j] =='B') { row[i]++; col[j]++ }
var ans = 0for (i in0 until m)
for (j in0 until n)
if (pic[i][j] =='B'&& row[i] ==1&& col[j] ==1) ans++return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
deffindLonelyPixel(self, pic: list[list[str]]) -> int:
m, n = len(pic), len(pic[0])
row = [0] * m
col = [0] * n
for i in range(m):
for j in range(n):
if pic[i][j] =='B':
row[i] +=1 col[j] +=1 ans =0for i in range(m):
for j in range(n):
if pic[i][j] =='B'and row[i] ==1and col[j] ==1:
ans +=1return ans
impl Solution {
pubfnfind_lonely_pixel(pic: Vec<Vec<char>>) -> i32 {
let m = pic.len();
let n = pic[0].len();
letmut row =vec![0; m];
letmut col =vec![0; n];
for i in0..m {
for j in0..n {
if pic[i][j] =='B' {
row[i] +=1;
col[j] +=1;
}
}
}
letmut ans =0;
for i in0..m {
for j in0..n {
if pic[i][j] =='B'&& row[i] ==1&& col[j] ==1 {
ans +=1;
}
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
findLonelyPixel(pic: string[][]):number {
constm=pic.length, n=pic[0].length;
constrow= Array(m).fill(0), col= Array(n).fill(0);
for (leti=0; i<m; i++)
for (letj=0; j<n; j++)
if (pic[i][j] ==='B') { row[i]++; col[j]++; }
letans=0;
for (leti=0; i<m; i++)
for (letj=0; j<n; j++)
if (pic[i][j] ==='B'&&row[i] ===1&&col[j] ===1) ans++;
returnans;
}
}