531. Lonely Pixel I
given изображение размером m x n, состоящее из чёрных ('B') и белых ('W') пикселей. return количество чёрных одиночных пикселей.
Чёрный одиночный пиксель — это символ 'B', расположенный в такой позиции, где в той же строке и в том же столбце нет других чёрных пикселей.
示例:
Input: picture = [["W","W","B"],["W","B","W"],["B","W","W"]]
Output: 3
Explanation: All the three 'B's are black lonely pixels.
C# 解法
匹配/原始public class Solution {
public int FindLonelyPixel(char[][] picture) {
int n = picture.Length;
int m = picture[0].Length;
int[] rowCount = new int[n];
int[] columnCount = new int[m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (picture[i][j] == 'B') {
rowCount[i]++;
columnCount[j]++;
}
}
}
int answer = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (picture[i][j] == 'B' && rowCount[i] == 1 && columnCount[j] == 1) {
answer++;
}
}
}
return answer;
}
}
C++ 解法
自动草稿,提交前请检查#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
public int FindLonelyPixel(char[][] picture) {
int n = picture.size();
int m = picture[0].size();
vector<int>& rowCount = new int[n];
vector<int>& columnCount = new int[m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (picture[i][j] == 'B') {
rowCount[i]++;
columnCount[j]++;
}
}
}
int answer = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (picture[i][j] == 'B' && rowCount[i] == 1 && columnCount[j] == 1) {
answer++;
}
}
}
return answer;
}
}
Java 解法
匹配/原始class Solution {
public int findLonelyPixel(char[][] picture) {
int n = picture.length;
int m = picture[0].length;
int rowCount[] = new int[n];
int columnCount[] = new int[m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (picture[i][j] == 'B') {
rowCount[i]++;
columnCount[j]++;
}
}
}
int answer = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (picture[i][j] == 'B' && rowCount[i] == 1 && columnCount[j] == 1) {
answer++;
}
}
}
return answer;
}
}
JavaScript 解法
匹配/原始var findLonelyPixel = function(picture) {
let n = picture.length;
let m = picture[0].length;
let rowCount = new Array(n).fill(0);
let columnCount = new Array(m).fill(0);
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
if (picture[i][j] === 'B') {
rowCount[i]++;
columnCount[j]++;
}
}
}
let answer = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
if (picture[i][j] === 'B' && rowCount[i] === 1 && columnCount[j] === 1) {
answer++;
}
}
}
return answer;
};
Python 解法
匹配/原始class Solution:
def findLonelyPixel(self, picture: List[List[str]]) -> int:
n = len(picture)
m = len(picture[0])
rowCount = [0] * n
columnCount = [0] * m
for i in range(n):
for j in range(m):
if picture[i][j] == 'B':
rowCount[i] += 1
columnCount[j] += 1
answer = 0
for i in range(n):
for j in range(m):
if picture[i][j] == 'B' and rowCount[i] == 1 and columnCount[j] == 1:
answer += 1
return answer
Go 解法
匹配/原始func findLonelyPixel(picture [][]byte) int {
n := len(picture)
m := len(picture[0])
rowCount := make([]int, n)
columnCount := make([]int, m)
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
if picture[i][j] == 'B' {
rowCount[i]++
columnCount[j]++
}
}
}
answer := 0
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
if picture[i][j] == 'B' && rowCount[i] == 1 && columnCount[j] == 1 {
answer++
}
}
}
return answer
}
Algorithm
Подсчёт количества чёрных пикселей в 字符串х и столбцах:
Пройдите по всей матрице picture, для каждой чёрной клетки (x, y) увеличивайте rowCount[x] и colCount[y] на 1.
Поиск одиночных чёрных пикселей:
Снова пройдите по всей матрице и для каждой чёрной клетки (x, y) проверьте значения rowCount[x] и colCount[y]. Если оба значения равны 1, увеличьте переменную answer на 1.
Возврат результата:
return answer.
😎
Vacancies for this task
活跃职位 with overlapping task tags are 已显示.