Lonely Pixel I

本文介绍了一种算法,用于在一个由黑白像素组成的二维数组中找到所有‘孤独’的黑色像素。孤独像素是指那些在同一行和同一列中没有其他黑色像素的黑色像素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Example:

Input: 
[['W', 'W', 'B'],
 ['W', 'B', 'W'],
 ['B', 'W', 'W']]

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

 

Note:

  1. The range of width and height of the input 2D array is [1,500].

 

 1 public class Solution {
 2     public int findLonelyPixel(char[][] picture) {
 3         if (picture == null || picture[0] == null) return 0;
 4         
 5         int m = picture.length, n = picture[0].length;
 6         boolean[] rows = new boolean[m];
 7         Arrays.fill(rows, true);
 8         boolean[] cols = new boolean[n];
 9         Arrays.fill(cols, true);
10         
11         for (int i = 0; i < m; i++) {
12             int rowCount = 0;
13             for (int j = 0; j < n; j++) {
14                 if (picture[i][j] == 'B')
15                     rowCount++;
16             }
17             if (rowCount > 1) rows[i] = false;
18         }
19         
20         for (int j = 0; j < n; j++) {
21             int colCount = 0;
22             for (int i = 0; i < m; i++) {
23                 if (picture[i][j] == 'B')
24                     colCount++;
25             }
26             if (colCount > 1) cols[j] = false;
27         }
28         
29         int result = 0;
30         for (int i = 0; i < m; i++) {
31             for (int j = 0; j < n; j++) {
32                 if (picture[i][j] == 'B' && rows[i] && cols[j])
33                     result++;
34             }
35         }
36         return result;
37     }
38 }

 

转载于:https://www.cnblogs.com/amazingzoe/p/6661168.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值