原题链接在这里:https://leetcode.com/problems/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:

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

题解:

Have a row array and column array to track how many B on the corresponding row or column.

Iterate the picture for the 1st time and update row and column.

Iterate the prictrue for the 2nd time to accumlate the count when both r[i] and c[j] == 1.

Time Complexity: O(m*n). m = picture.length. n = picture[0].length.

Space: O(m+n).

AC Java:

 class Solution {
public int findLonelyPixel(char[][] picture) {
if(picture == null || picture.length == 0 || picture[0].length == 0){
return 0;
} int m = picture.length;
int n = picture[0].length;
int [] r = new int[m];
int [] c = new int[n];
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(picture[i][j] == 'B'){
r[i]++;
c[j]++;
}
}
} int res = 0;
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(picture[i][j] == 'B' && r[i] == 1 && c[j] == 1){
res++;
}
}
} return res;
}
}

LeetCode 531. Lonely Pixel I的更多相关文章

  1. [LeetCode] 531. Lonely Pixel I 孤独的像素 I

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

  2. [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 ...

  3. LeetCode 531. Longly Pixel I (孤独的像素之一) $

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

  4. 531. Lonely Pixel I

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

  5. LeetCode 533. Lonely Pixel II (孤独的像素之二) $

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  6. [LeetCode] Lonely Pixel II 孤独的像素之二

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  7. [LeetCode] Lonely Pixel I 孤独的像素之一

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

  8. 533. Lonely Pixel II

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. SQL分类之DDL:操作数据库表

    DDL:操作数据库表 1.操作数据库:CRUD 1.C(Create):创建 创建数据库: create database 数据库名称 创建数据库,判断不存在,再创建: create database ...

  2. pandas 学习 第十一篇:处理缺失值

    Pandas中的缺失值是指nan.None和NaT.如果需要把inf 和 -inf视为缺失值,需要设置 pandas的选项: pandas.options.mode.use_inf_as_na = T ...

  3. bootstrap table--面相配置、hook、适配的表格框架

    bootstrap table--面相配置.hook.适配的表格框架

  4. EntityFramework执行Add-Miragtion或者Update-Database出闲ScriptHalted

    今天基友在他本机运行我的项目,执行ef还原数据的时候,一直出现ScriptHalted的错误. 找了下百度,只找到一个stackoverflowd的答案,没有看到中文的解决方案. 现在总结一下,并给出 ...

  5. 【转】 python_控制台输出带颜色的文字方法

    在python开发的过程中,经常会遇到需要打印各种信息.海量的信息堆砌在控制台中,就会导致信息都混在一起,降低了重要信息的可读性.这时候,如果能给重要的信息加上字体颜色,那么就会更加方便用户阅读了. ...

  6. 【转载】Jupyter Notebook 常用快捷键

    原文:http://blog.csdn.net/lawme/article/details/51034543 Jupyter Notebook 有两种键盘输入模式.编辑模式,允许你往单元中键入代码或文 ...

  7. ES6 字符串&正则表达式

    目录 第二章 字符串和正则表达式UTF-16码位codePointAt()方法String.fromCodePoint()方法normalize()方法正则表达式u修饰符其他字符串变更字符串中的字串识 ...

  8. 微信小程序调用云函数出错 Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail cloud function service error code -501005, error message Environment not found;

    错误异常: Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail cloud ...

  9. k8s网络配置管理

    docker容器的四种网络类型 1.桥接           2.联盟    3.主机    4.无 docker跨节点的容器通信必须通过NAT机制  宿主机上的容器一般都是私网地址 它可以通过宿主机 ...

  10. docker命令小全 this is my note.

    服务器类型:linux =>centos 7.X以上版本 常用命令使用紫色加粗标明 1.安装yum-util(为配置docker安装时使用阿里镜像做准备):yum install -y yum- ...