[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 ...
随机推荐
- java操作数据库的通用的类
package cn.dao; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; ...
- PHP 密码重置,发送邮件,随机长度字母数字密码
<?php include ("database.php"); require_once ('email.class.php'); date_default_timezone ...
- .Net开发之旅(一个年少轻狂的程序员的感慨)
高端大气上档次.这次当时一个身为懵懂初中生的我对程序员这一职位的描述.那时虽不是随处都能看到黑客大军的波及,但至少是知道所谓的黑客爸爸的厉害,一言不合说被黑就被黑.对于懵懂的我那是一种向往.自己也曾想 ...
- 【RabbitMQ系列】 Spring mvc整合RabbitMQ
一.linux下安装rabbitmq 1.安装erlang环境 wget http://erlang.org/download/otp_src_18.2.1.tar.gz tar xvfz otp_s ...
- 【LATEX】个人版latex论文模板
以下是我的个人论文模板,运行环境为Xelatex(在线ide:Sharelatex.com) 鉴于本人常有插入程序的需求,故引用了lstlisting \RequirePackage{ifxetex} ...
- java关于for循环。
众所周知,JAVA中for循环的基本格式为: for(初始化表达式:布尔表达式:循环后更新表达式){循环体} 举个例子来说可以写成 (1)for (int x=1;x<10;x++){ Syst ...
- IDEA之Jrebel插件激活
问题: 码农日常中,热部署是必不可少的,而jrebel插件很好的实现热部署功能. IDEA下载jrebel插件,可以免费试用15天,但之后就无法使用.因为Jrebel是收费的. 解决方法: 楼主也是百 ...
- linux作为服务器,利用top命令查看服务进程的耗用情况
top命令查看进程服务如下: 其中shift+m可以按照内存的消耗进行排序,shift+p是按照cpu的消耗进程,排序,其中对cpu的消耗是一定时间,谁占用的时间越长消耗越大, 还有按空格键,会刷新一 ...
- python 模块部分补充知识
一.hashlib hashlib 模块主要用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法. 实例 ...
- 常用cmd代码片段及.net core打包脚本分享
bat基础命令 注释:rem 注释~~ 输出:echo hello world 接收用户输入:%1 %2,第n个变量就用%n表示 当前脚本路径:%~dp0 当前目录路径:%cd% 设置变量:set c ...