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. html css 前端基础 学习方法及经验分享

    前言: 入园第一天,想分享一点儿前端基础html css 的学习方法和一些经验给热爱前端,但是不知道从哪儿开始,依旧有些迷茫的新手朋友们.当然,适合每个人的学习方式不同,以下所讲的仅供参考. 一.关于 ...

  2. 通过Excel认识POI

    1.POI是什么 Apache POI - the Java API for Microsoft Documents,顾名思义,Apache的三方包,用来操作微软office文档的,多数时候用来操作e ...

  3. 验证整数 Double 转 int 两种写法

    Double  转int 1)之前一直是使用强转 Double num = Double.parseDouble(object.toString());   int n = (int)num;   i ...

  4. 自学Unity3D 之 贪吃蛇 添加摄像机跟随

    在Unity的世界中, 物体的位置都是由向量构成的. 今天所需要做的就是让摄像机保持跟蛇头的相对距离. 首先  设蛇头的位置在A 点  , 摄像机的位置在B 点 则  我们可以知道  他们的offse ...

  5. Ubuntu16.04.1上搭建分布式的Redis集群

    为什么要集群: 通常为了,提高网站的响应速度,总是把一些经常用到的数据放到内存中,而不是放到数据库中,Redis是一个很好的Cache工具,当然了还有Memcached,这里只讲Redis.在我们的电 ...

  6. AngularJS的$rootScope和$scope联系和区别

    scope是html和单个controller之间的桥梁,数据绑定就靠他了. rootscope是各个controller中scope的桥梁.用rootscope定义的值,可以在各个controlle ...

  7. UIScollview 添加UICollectionView 实现放大缩小

    创建一个空的工程 打开storyboard,添加UIScollview 设置代理 实现代理方法 - (UIView *)viewForZoomingInScrollView:(UIScrollView ...

  8. 使用phpmailer插件发邮件失败提示:SMTP -> ERROR: Failed to connect to server: Connection timed out (110) smtp connect() failed;

    一个邮件发送问题,整整弄了我一周时间,起因是这样的,之前弄的一个网站,需要在邮箱里面认证之后才可以注册成功.网站上线了差不多一年之后,客户突然跟我说,网站不能注册了,然后我就查看了一下代码. 发现报这 ...

  9. AFN默认请求和响应的处理

       1.默认的响应的解析      1.1 AFN默认不支持接受text/html数据类型,只需要增加即可     manager.responseSerializer.acceptableCont ...

  10. EXIT_SUCCESS EXIT_FAILURE

    在vc++6.0下头文件stdlib.h中定义如下: /* Definition of the argument values for the exit() function */ #define E ...