[LeetCode] Lonely Pixel I 孤独的像素之一
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:
- The range of width and height of the input 2D array is [1,500].
这道题定义了一种孤独的黑像素,就是该黑像素所在的行和列中没有其他的黑像素,让我们找出所有的这样的像素。那么既然对于每个黑像素都需要查找其所在的行和列,为了避免重复查找,我们可以统一的扫描一次,将各行各列的黑像素的个数都统计出来,然后再扫描所有的黑像素一次,看其是否是该行该列唯一的存在,是的话就累加计数器即可,参见代码如下:
class Solution {
public:
int findLonelyPixel(vector<vector<char>>& picture) {
if (picture.empty() || picture[].empty()) return ;
int m = picture.size(), n = picture[].size(), res = ;
vector<int> rowCnt(m, ), colCnt(n, );
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (picture[i][j] == 'B') {
++rowCnt[i];
++colCnt[j];
}
}
}
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (picture[i][j] == 'B') {
if (rowCnt[i] == && colCnt[j] == ) {
++res;
}
}
}
}
return res;
}
};
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Lonely Pixel I 孤独的像素之一的更多相关文章
- [LeetCode] Lonely Pixel II 孤独的像素之二
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- [LeetCode] 531. Lonely Pixel I 孤独的像素 I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- [LeetCode] 533. Lonely Pixel II 孤独的像素 II
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- LeetCode 531. Longly Pixel I (孤独的像素之一) $
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- LeetCode 531. Lonely Pixel I
原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...
- LeetCode 531----Lonely Pixel I----两种算法之间性能的比较
Lonely Pixel I 两种算法之间的性能比较 今天参加LeetCode Weekly Contest 22,第二题 "Lonely Pixel I" 问题描述如下: Giv ...
- 531. Lonely Pixel I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- 像素 PIXEL 图片的基本单位 像素非常小 图片是成千上万的像素组成 显示/屏幕分辨率 (DPI 屏幕分辨率)
像素 PIXEL 图片的基本单位 像素非常小 图片是成千上万的像素组成 显示/屏幕分辨率 (DPI 屏幕分辨率) 图像分辨率 (PPI) 1920*1080是像素点长度1920个像素点 X1080个像 ...
- LeetCode 533. Lonely Pixel II (孤独的像素之二) $
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
随机推荐
- Tomcat 8启动速度慢原因1: At least one JAR was scanned for TLDs yet contained no TLDs
最近使用tomcat8启动项目时,发现At least one JAR was scanned for TLDs yet contained no TLDs这一步加载时间非常长, 从网上收集了各种资料 ...
- Android破解心得——记学习七少月安卓大型安全公开课
第一课 讲解了关于在安卓破解之中环境的配置及所需要用到的软件,重要的软件是Androidkiller,安卓逆向助手 第二课讲解了java与smali的关系,从smail角度详细的分析了一个简单的Hel ...
- 浅谈RMQ
RMQ是一类求区间极值的问题 有一种 \(O\left(nlogn\right)\) 的解法,用倍增实现 倍增算法 变量的定义 \(A_i\) : 原数组 \(f_{i,j}\) : 以 \(i\) ...
- Property 'id' not found on type java.lang.String
改为 忘写了$符,取不出来,因此报错!
- [福大软工] W班 总成绩排行榜
评分链接 作业1 作业2 作业3 作业4 总分排名
- 福州大学W班-Beta冲刺评分
作业链接 https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/1428 作业要求 1.博客具体要求 昨天的困 ...
- Java暑假作业
一.电影观后感 电影<摔跤吧!爸爸>观后感 二.下学期的计划与目标 大一学年总结: 参与了大大小小的学院活动,例如机器人搭建.辩论赛,也参加了学生会的部门,参与了组织活动.通过参与活动获 ...
- c/cpp语言链表连接部分详解
核心代码: ①pTail->next = pNew; ②pNew->next = NULL; ③pTail = pNew; 设结构体名称为 struct ST: 注:方框代表分配的内存空间 ...
- 关于安装win7系统时出现0x0000007b电脑蓝屏代码的问题
问题解析: 0X0000007B 这个错误网上都说是sata硬盘的什么引导模式的原因引起. 在网上查找了很久,大概引起错误的原因就是:sata和ide两种模式不同,前者可以装win7系统,后者是xp系 ...
- nyoj 孪生素数
孪生素数问题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 写一个程序,找出给出素数范围内的所有孪生素数的组数.一般来说,孪生素数就是指两个素数距离为2,近的不能再 ...