原题链接在这里: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. 7. Scala面向对象编程(中级部分)

    7.1 包 7.1.1 看一个应用场景 现在有两个程序员共同开发一个项目,程序员xiaoming希望定义一个类取名Dog,程序员xiaohong也想定一个类也叫Dog,两个程序员还为此吵了起来,该怎么 ...

  2. ZYNQ笔记(7):AXI从口自定义IP封装

    使用 AXI_Lite 从口实现寄存器列表的读写,并且自己封装为一个自定义 IP,以便以后使用.本次记录的是 M_AXI_GP0 接口,此接口是 ARM 作为主机,FPGA 作为从机,配置 FPGA ...

  3. c/c++封装成python包

    参考网址:https://blog.csdn.net/tiankongtiankong01/article/details/80420033 SWIG (Simplified Wrapper and ...

  4. shell-快速入门_批处理脚本编程语言

    1. Shell概述 1.1. Shell是什么 Shell是一门批处理脚本编程语言. 批处理是什么? 操作系统都分为(GUI)图形界面,命令界面(command). 命令操作的可以不用一一条执行.可 ...

  5. 【简记】修改Docker数据目录位置,包含镜像位置

    为啥要改? Docker安装后默认下载的位置在/var/lib/docker ,如果/var分区没有独立分出来,Linux下默认是与/根分区在一起.一般我们装Linux系统的时候,除了做邮件服务器外, ...

  6. EF Core 批处理语句

    在Entity Framework Core (EF Core)有许多新的功能,最令人期待的功能之一就是批处理语句.那么批处理语句是什么呢?批处理语句意味着它不会为每个插入/更新/删除语句发送单独的请 ...

  7. css z-index 的学习

    前言:这是笔者第一次写博客,主要是学习之后自己的理解.如果有错误或者疑问的地方,请大家指正,我会持续更新! z-index属性描述元素的堆叠顺序(层级),意思是 A 元素可以覆盖 B 元素,但是 B ...

  8. MethodInvoker委托,跨线程访问

    Invoke(new MethodInvoker(delegate { textBox1.Enabled = true; })); 上面是简单缩写,也可以写成 private void btnOK_C ...

  9. python3.6安装pyinstaller报错:AttributeError: module 'enum' has no attribute 'IntFlag'

    转载至:https://blog.csdn.net/qq_41185868/article/details/80599336 感谢原作者的分享 解决思路:这可能是由包Enum34引起的.因为Pytho ...

  10. 一步一步学Spring Boot 2 微服务项目实战 - 黄文毅-2018年8月第一次印刷

    properties 配置文件的优先级高于.yml .在properties文件中配置了server.port=8080 同时在.yml中配置了server.port=8090  Spring Boo ...