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].

题目标签:Array

  题目给了我们一个2d picture array,让我们找出有几个孤独的像素B。孤独的像素B的行和列中只有自己一个像素。

  可以建立2个 array, 分别是 row 和 col, 它们的size 就是picture里的行和列的size。

  第一次遍历picture:如果是B,就把B的row 和 column 在 row array 和 col array 里对应位置 加1。目的是为了记录每一行和每一列里有多少个B。

  第二次遍历picture:如果是B,就到对应的 row  和 col array 里, 如果 row 和 col 的值都是1 的话,说明这一个B 是 row 这一行里, 和 col 这一列里唯一的B。累计计数到res。

Java Solution:

Runtime beats 80.41%

完成日期:09/24/2017

关键词:Array

关键点:设立row array & col array 记录每一行每一列里有多少个B;孤单像素所在的行和列只有它自己

 class Solution
{
public int findLonelyPixel(char[][] picture)
{
int n = picture.length;
int m = picture[0].length;
// create two array for counting B
int[] row = new int[n];
int[] col = new int[m]; int res = 0; // counts longly black // 1st iteration, if find a B, increase its row and col number in two array
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(picture[i][j] == 'B')
{
row[i]++;
col[j]++;
}
}
} // 2nd iteration, if find a B, check its row and col are both 1, meaning lonly B
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(picture[i][j] == 'B' && row[i] == 1 && col[j] == 1)
res++; return res;
}
}

参考资料:

https://discuss.leetcode.com/topic/81680/java-o-nm-time-with-o-n-m-space-and-o-1-space-solutions

LeetCode 题目列表 - LeetCode Questions List

LeetCode 531. Longly 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] Lonely Pixel II 孤独的像素之二

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

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

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

  5. LeetCode 531. Lonely Pixel I

    原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...

  6. 像素 PIXEL 图片的基本单位 像素非常小 图片是成千上万的像素组成 显示/屏幕分辨率 (DPI 屏幕分辨率)

    像素 PIXEL 图片的基本单位 像素非常小 图片是成千上万的像素组成 显示/屏幕分辨率 (DPI 屏幕分辨率) 图像分辨率 (PPI) 1920*1080是像素点长度1920个像素点 X1080个像 ...

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

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

  8. 531. Lonely Pixel I

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

  9. Efficient and Accurate Arbitrary-Shaped Text Detection with Pixel Aggregation Network(利用像素聚合网络进行高效准确的任意形状文本检测)

    PSENet V2昨日刚出,今天翻译学习一下. 场景文本检测是场景文本阅读系统的重要一步,随着卷积神经网络的快速发展,场景文字检测也取得了巨大的进步.尽管如此,仍存在两个主要挑战,它们阻碍文字检测部署 ...

随机推荐

  1. Struts2第三篇【Action开发方式、通配符、Struts常量、跳转全局视图、action节点默认配置】

    前言 上篇Struts博文已经讲解了Struts的开发步骤以及执行流程了-..对Struts的配置文件有了了解-..本博文继续讲解Struts在配置的时候一些值得要学习的细节- Action开发的三种 ...

  2. hadoop2.0的数据副本存放策略

    在hadoop2.0中,datanode数据副本存放磁盘选择策略有两种方式: 第一种是沿用hadoop1.0的磁盘目录轮询方式,实现类:RoundRobinVolumeChoosingPolicy.j ...

  3. [UIKit学习]05.关于plist

    plist是一种iOS本地化轻量级存储方式 创建plist 选择New File-> Resource->plist 加载plist //获得Plist文件的全路径 NSBundle *b ...

  4. ACM学习之路___HDU 2066 一个人的旅行

    Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还 ...

  5. 在0~N个数字中,取指定个数的不重复数字,要求这些数字的和为指定值,求所有结果

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  6. java 如何将方法作为传参--多态

    在前段时研究智能算法时,发现如果使用java进行实现的话,往往具体实现过程差不多,但是适应值函数却根据 研究对象的不同发生很大的改变,这样对代码的维护产生很大的阻碍,于是产生的一个疑问:可不可以将适 ...

  7. Java 制作证书的工具keytool用法总结

    一.keytool的概念 keytool 是个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务.在 ...

  8. 常用git指令

    git checkout -b newBranchName //与当前分支内容相同! git checkout -b 本地分支 origin xxx//远程分支 在本地新建一个分支,并把远程分支的代码 ...

  9. TCP/IP(二)物理层详解

    前言 在前面说了一下,计算机网络的大概内容,没有去深刻的去了解它,这篇文章给大家分享一下物理层! 我们知道ISO模型是七层,TCP/IP模型是五层,而tcp/ip协议只将七层概括为4层,我们将学习其中 ...

  10. 第一个ExtJS练习(添加用户面板)

    1.[准备] 我是在visual studio里面建立了一个asp.net MVC项目,然后导入ExtJS必要的包,然后写的. ExtJS5.1版本下载:https://pan.baidu.com/s ...