Given a picture consisting of black and white pixels, find the number of black lonely pixels.
The picture is represented by a 2D char array consisting of ‘B’ and ‘W’, which means black and white pixels respectively.
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.
示例
示例:
输入:
[['W', 'W', 'B'],
['W', 'B', 'W'],
['B', 'W', 'W']]
输出: 3
解析: 全部三个'B'都是黑色孤独像素。
注意:
输入二维数组行和列的范围是 [1,500]。
解题
class Solution {
public int findLonelyPixel(char[][] picture) {
int ans = 0;
int rowN = picture.length;
int colN = picture[0].length;
int[] row = new int[rowN];
int[] col = new int[colN];
List<Integer[]> list = new ArrayList<>();
for (int i = 0; i < rowN; i++) {
for (int j = 0; j < colN; j++) {
if (picture[i][j] == 'B') {
row[i]++;
col[j]++;
list.add(new Integer[]{i, j});
}
}
}
for (Integer[] blackPoint : list) {
int i = blackPoint[0];
int j = blackPoint[1];
if (row[i] == 1 && row[i] == col[j]) {
ans++;
}
}
return ans;
}
}
本篇文章来源于微信公众号:程序IT圈
原创文章,作者:栈长,如若转载,请注明出处:https://www.cxyquan.com/19777.html