原题链接在这里: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. Logback获取SkyWalking的全局唯一标识 trace-id 记录到日志中

    为什么要获取trace-id 通过上文Docker-Compose搭建单体SkyWalking我们搭建了SkyWalking服务,我们需要在日志中记录下来每次请求的唯一标识(trace-id),这样就 ...

  2. python操作jenkins、python-jenkins api

    Jenkins作为最流行的自动化流程的核心工具,我们使用它自带的web-ui完全可以满足日常的构建及发布工作,但是如果需要和其他系统做集成就必须二次开发或者通过API方式进行交互了. Jenkins介 ...

  3. Mysql之索引(六)

    1.思考 在图书馆是怎么找到一本书的? 一般的应用系统对比数据库的读写比例在10:1左右(即有10次查询有1次写操作),而且插入操作和更新操作很少出现性能问题. 遇到最多,最复杂的还是一些复杂的查询操 ...

  4. 换个语言学一下 Golang (13)——Web表单处理

    介绍 表单是我们平常编写Web应用常用的工具,通过表单我们可以方便的让客户端和服务器进 行数据的交互.对于以前开发过Web的用户来说表单都非常熟悉.表单是一个包含表单元素的区域.表单元素是允许用户在表 ...

  5. 【转载】C#如何往DataTable中新增一个数据列

    在C#中的Datatable数据变量的操作过程中,有时候我们需要往现有的DataTable中新增一个自定义数据列,该列在原有的DataTable变量中并不存在,属于用户手工自定义新增的数据列,在往Da ...

  6. vue组件5 组件和v-for指令

    使用v-for遍历一个数组的时候,并且给定的数组变化时vue不会重复生成所有的元素,而是智能的找到需要更改的元素,并只改变这些元素 key属性可以告诉vue数组中的每个元素都应该与页面上的哪个元素相关 ...

  7. jquery问题,如何调用带this的函数?

    这样写: 1 2 3 4 5 6 7 8 9 10 11 12 $(".aa").on("mouseout",function(){     var obj = ...

  8. underscore_1: map()

    map()是underscore.js中一个处理数组和对象的方法. params: 1. array || obj 2. callback 3. content 上下文指向 使用: var obj = ...

  9. Java 数组(一)定义与访问

    一.数组 1.容器概述    容器:是将多个数据存储到一起,每个数据称为该容器的元素. 2.数组概述  数组:数组就是存储数据长度固定的容器,保证多个数据的数据类型要一致. 数组特点: (1)数组是一 ...

  10. HttpUtils请求工具类

    package com.cmcc.hybj.payment.framework.https; import java.io.UnsupportedEncodingException;import ja ...