返回介绍

solution / 0500-0599 / 0531.Lonely Pixel I / README_EN

发布于 2024-06-17 01:03:59 字数 4502 浏览 0 评论 0 收藏 0

531. Lonely Pixel I

中文文档

Description

Given an m x n picture consisting of black 'B' and white 'W' pixels, return _the number of black lonely pixels_.

A black lonely pixel is a character 'B' that located at a specific position where the same row and same column don't have any other black pixels.

 

Example 1:

Input: picture = [["W","W","B"],["W","B","W"],["B","W","W"]]
Output: 3
Explanation: All the three 'B's are black lonely pixels.

Example 2:

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

 

Constraints:

  • m == picture.length
  • n == picture[i].length
  • 1 <= m, n <= 500
  • picture[i][j] is 'W' or 'B'.

Solutions

Solution 1

class Solution:
  def findLonelyPixel(self, picture: List[List[str]]) -> int:
    m, n = len(picture), len(picture[0])
    rows, cols = [0] * m, [0] * n
    for i in range(m):
      for j in range(n):
        if picture[i][j] == 'B':
          rows[i] += 1
          cols[j] += 1
    res = 0
    for i in range(m):
      if rows[i] == 1:
        for j in range(n):
          if picture[i][j] == 'B' and cols[j] == 1:
            res += 1
            break
    return res
class Solution {
  public int findLonelyPixel(char[][] picture) {
    int m = picture.length, n = picture[0].length;
    int[] rows = new int[m];
    int[] cols = new int[n];
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (picture[i][j] == 'B') {
          ++rows[i];
          ++cols[j];
        }
      }
    }
    int res = 0;
    for (int i = 0; i < m; ++i) {
      if (rows[i] == 1) {
        for (int j = 0; j < n; ++j) {
          if (picture[i][j] == 'B' && cols[j] == 1) {
            ++res;
            break;
          }
        }
      }
    }
    return res;
  }
}
class Solution {
public:
  int findLonelyPixel(vector<vector<char>>& picture) {
    int m = picture.size(), n = picture[0].size();
    vector<int> rows(m);
    vector<int> cols(n);
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (picture[i][j] == 'B') {
          ++rows[i];
          ++cols[j];
        }
      }
    }
    int res = 0;
    for (int i = 0; i < m; ++i) {
      if (rows[i] == 1) {
        for (int j = 0; j < n; ++j) {
          if (picture[i][j] == 'B' && cols[j] == 1) {
            ++res;
            break;
          }
        }
      }
    }
    return res;
  }
};
func findLonelyPixel(picture [][]byte) int {
  m, n := len(picture), len(picture[0])
  rows := make([]int, m)
  cols := make([]int, n)
  for i := 0; i < m; i++ {
    for j := 0; j < n; j++ {
      if picture[i][j] == 'B' {
        rows[i]++
        cols[j]++
      }
    }
  }
  res := 0
  for i := 0; i < m; i++ {
    if rows[i] == 1 {
      for j := 0; j < n; j++ {
        if picture[i][j] == 'B' && cols[j] == 1 {
          res++
          break
        }
      }
    }
  }
  return res
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文