531. Lonely Pixel I

Văn bản bài toán được dịch từ tiếng Nga theo ngôn ngữ giao diện. Mã không thay đổi.

given изображение размером m x n, состоящее из чёрных ('B') и белых ('W') пикселей. return количество чёрных одиночных пикселей.

Чёрный одиночный пиксель — это символ 'B', расположенный в такой позиции, где в той же строке и в том же столбце нет других чёрных пикселей.

Ví dụ:

Input: picture = [["W","W","B"],["W","B","W"],["B","W","W"]]

Output: 3

Explanation: All the three 'B's are black lonely pixels.

C# lời giải

đã khớp/gố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++ lời giải

bản nháp tự động, xem lại trước khi gửi
#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 lời giải

đã khớp/gốc
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 lời giải

đã khớp/gốc
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 lời giải

đã khớp/gốc
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 lời giải

đã khớp/gốc
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

Подсчёт количества чёрных пикселей в chuỗiх и столбцах:

Пройдите по всей матрице picture, для каждой чёрной клетки (x, y) увеличивайте rowCount[x] и colCount[y] на 1.

Поиск одиночных чёрных пикселей:

Снова пройдите по всей матрице и для каждой чёрной клетки (x, y) проверьте значения rowCount[x] и colCount[y]. Если оба значения равны 1, увеличьте переменную answer на 1.

Возврат результата:

return answer.

😎

Vacancies for this task

việc làm đang hoạt động with overlapping task tags are đã hiển thị.

Tất cả việc làm
Chưa có việc làm đang hoạt động.