class Solution {
public:
int findLonelyPixel(vector<vector<char>>& picture) {
}
};
531. 孤独像素 I
给你一个大小为 m x n
的图像 picture
,图像由黑白像素组成,'B'
表示黑色像素,'W'
表示白色像素,请你统计并返回图像中 黑色 孤独像素的数量。
黑色孤独像素 的定义为:如果黑色像素 'B'
所在的同一行和同一列不存在其他黑色像素,那么这个黑色像素就是黑色孤独像素。
示例 1:
输入:picture = [["W","W","B"],["W","B","W"],["B","W","W"]] 输出:3 解释:全部三个 'B' 都是黑色的孤独像素
示例 2:
输入:picture = [["B","B","B"],["B","B","W"],["B","B","B"]] 输出:0
提示:
m == picture.length
n == picture[i].length
1 <= m, n <= 500
picture[i][j]
为 'W'
或 'B'
相似题目
原站题解
java 解法, 执行用时: 2 ms, 内存消耗: 43 MB, 提交时间: 2023-10-22 00:00:49
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; } }
python3 解法, 执行用时: 76 ms, 内存消耗: 21.3 MB, 提交时间: 2023-10-22 00:00:18
class Solution: def findLonelyPixel2(self, picture: List[List[str]]) -> int: row, col = [collections.Counter(r)['B'] for r in picture], [collections.Counter(c)['B'] for c in zip(*picture)] return sum((row[r] == 1 and col[c] == 1 and picture[r][c] == 'B') for r in range(len(picture)) for c in range(len(picture[0]))) def findLonelyPixel(self, picture: List[List[str]]) -> int: R, C = len(picture), len(picture[0]) row_sum = [0 for _ in range(R)] col_sum = [0 for _ in range(C)] for r in range(R): for c in range(C): if picture[r][c] == 'B': row_sum[r] += 1 col_sum[c] += 1 res = 0 for r in range(R): if row_sum[r] == 1: for c in range(C): if picture[r][c] == 'B' and col_sum[c] == 1: res += 1 return res
golang 解法, 执行用时: 4 ms, 内存消耗: 5.7 MB, 提交时间: 2023-10-21 23:59:30
func findLonelyPixel(picture [][]byte) int { x, y := map[int]int{}, map[int]int{} for i := 0; i < len(picture); i++ { for j := 0; j < len(picture[i]); j++ { if picture[i][j] == 'B' { x[i]++ y[j]++ } } } ret := 0 for i := 0; i < len(picture); i++ { for j := 0; j < len(picture[i]); j++ { if picture[i][j] == 'B' && x[i] == 1 && y[j] == 1 { ret++ } } } return ret }
cpp 解法, 执行用时: 72 ms, 内存消耗: 16.5 MB, 提交时间: 2023-10-21 23:59:12
class Solution { public: int findLonelyPixel(vector<vector<char>>& picture) { int n=(int)picture.size(),m=(int)picture[0].size(); int ans=0; for (int i=0;i<n;++i){ for (int j=0;j<m;++j)if(picture[i][j]=='B'){ int col=0,row=0; for (int k=0;k<m;++k){ row+=picture[i][k]=='B'; } for (int k=0;k<n;++k){ col+=picture[k][j]=='B'; } if (row==1 && col==1) ans++; } } return ans; } }; class Solution2 { int row[505],col[505]; public: int findLonelyPixel(vector<vector<char>>& picture) { int n=(int)picture.size(),m=(int)picture[0].size(); for (int i=0;i<n;++i){ for (int j=0;j<m;++j){ row[i]+=picture[i][j]=='B'; col[j]+=picture[i][j]=='B'; } } int ans=0; for (int i=0;i<n;++i)if(row[i]==1){ for (int j=0;j<m;++j)if(picture[i][j]=='B'){ ans+=col[j]==1; } } return ans; } };