Given a picture consisting of black and white pixels, and a positive integer N, find the number of black pixels located at some specific row R and column C that align with all the following rules:

  1. Row R and column C both contain exactly N black pixels.
  2. For all rows that have a black pixel at column C, they should be exactly the same as row R

The picture is represented by a 2D char array consisting of 'B' and 'W', which means black and white pixels respectively.

Example:

Input:
[['W', 'B', 'W', 'B', 'B', 'W'],
['W', 'B', 'W', 'B', 'B', 'W'],
['W', 'B', 'W', 'B', 'B', 'W'],
['W', 'W', 'B', 'W', 'B', 'W']] N = 3
Output: 6
Explanation: All the bold 'B' are the black pixels we need (all 'B's at column 1 and 3).
0 1 2 3 4 5 column index
0 [['W', 'B', 'W', 'B', 'B', 'W'],
1 ['W', 'B', 'W', 'B', 'B', 'W'],
2 ['W', 'B', 'W', 'B', 'B', 'W'],
3 ['W', 'W', 'B', 'W', 'B', 'W']]
row index Take 'B' at row R = 0 and column C = 1 as an example:
Rule 1, row R = 0 and column C = 1 both have exactly N = 3 black pixels.
Rule 2, the rows have black pixel at column C = 1 are row 0, row 1 and row 2. They are exactly the same as row R = 0.

Note:

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

这道题是之前那道Lonely Pixel I的拓展,我开始以为这次要考虑到对角线的情况,可是这次题目却完全换了一种玩法。给了一个整数N,说对于均含有N个个黑像素的某行某列,如果该列中所有的黑像素所在的行都相同的话,该列的所有黑像素均为孤独的像素,让我们统计所有的这样的孤独的像素的个数。那么跟之前那题类似,我们还是要统计每一行每一列的黑像素的个数,而且由于条件二中要比较各行之间是否相等,如果一个字符一个字符的比较写起来比较麻烦,我们可以用个trick,把每行的字符连起来,形成一个字符串,然后直接比较两个字符串是否相等会简单很多。然后我们遍历每一行和每一列,如果某行和某列的黑像素刚好均为N,我们遍历该列的所有黑像素,如果其所在行均相等,则说明该列的所有黑像素均为孤独的像素,将个数加入结果res中,然后将该行的黑像素统计个数清零,以免重复运算,这样我们就可以求出所有的孤独的像素了,参见代码如下:

解法一:

class Solution {
public:
int findBlackPixel(vector<vector<char>>& picture, int N) {
if (picture.empty() || picture[].empty()) return ;
int m = picture.size(), n = picture[].size(), res = , k = ;
vector<int> rowCnt(m, ), colCnt(n, );
vector<string> rows(m, "");
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
rows[i].push_back(picture[i][j]);
if (picture[i][j] == 'B') {
++rowCnt[i];
++colCnt[j];
}
}
}
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (rowCnt[i] == N && colCnt[j] == N) {
for (k = ; k < m; ++k) {
if (picture[k][j] == 'B') {
if (rows[i] != rows[k]) break;
}
}
if (k == m) {
res += colCnt[j];
colCnt[j] = ;
}
}
}
}
return res;
}
};

看到论坛中的比较流行的解法是用哈希表来做的,建立黑像素出现个数为N的行和其出现次数之间的映射,然后我们就只需要统计每列的黑像素的个数,然后我们遍历哈希表,找到出现次数刚好为N的行,说明矩阵中有N个相同的该行,而且该行中的黑像素的个数也刚好为N个,那么第二个条件就已经满足了,我们只要再满足第一个条件就行了,我们在找黑像素为N个的列就行了,有几列就加几个N即可,参见代码如下:

解法二:

class Solution {
public:
int findBlackPixel(vector<vector<char>>& picture, int N) {
if (picture.empty() || picture[].empty()) return ;
int m = picture.size(), n = picture[].size(), res = ;
vector<int> colCnt(n, );
unordered_map<string, int> u;
for (int i = ; i < m; ++i) {
int cnt = ;
for (int j = ; j < n; ++j) {
if (picture[i][j] == 'B') {
++colCnt[j];
++cnt;
}
}
if (cnt == N) ++u[string(picture[i].begin(), picture[i].end())];
}
for (auto a : u) {
if (a.second != N) continue;
for (int i = ; i < n; ++i) {
res += (a.first[i] == 'B' && colCnt[i] == N) ? N : ;
}
}
return res;
}
};

类似题目:

Lonely Pixel I

参考资料:

https://discuss.leetcode.com/topic/81686/verbose-java-o-m-n-solution-hashmap/2

https://discuss.leetcode.com/topic/87164/a-c-solution-based-on-the-top-rated-issue

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

[LeetCode] Lonely Pixel II 孤独的像素之二的更多相关文章

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

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

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

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

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

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

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

  5. LeetCode 533----Lonely Pixel II

    问题描述 Given a picture consisting of black and white pixels, and a positive integer N, find the number ...

  6. 533. Lonely Pixel II

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

  7. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  8. [LeetCode] Redundant Connection II 冗余的连接之二

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  9. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

随机推荐

  1. 01_搭建Linux虚拟机(下)_我的Linux之路

    原文发布在特克斯博客www.susmote.com ​ 上一节已经给大家讲解了如何用VMware安装虚拟机,但是只讲了在VMware里面的操作 接下来我们讲在Linux内部的安装步骤 首先我们启动Li ...

  2. Java基础学习笔记十三 常用API之正则表达式、Date、DateFormat、Calendar

    正则表达式 正则表达式(英语:Regular Expression,在代码中常简写为regex).正则表达式是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个句法规则的字符串.在 ...

  3. 云计算之路-阿里云上-容器难容:优化自建 docker swarm 集群的部署

    在上周六遭遇阿里云容器服务 swarm 版的故障之后,我们决定还是走自建 docker swarm 之路,只要不是阿里云底层的问题,我们相信会找到办法解决或避开自建 docker swarm 不稳定的 ...

  4. 第2次作业:STEAM案例分析

    1.介绍产品的相关信息 1.1我选择的产品是STEAM 1.2选择STEAM的理由 STEAM是一个线上游戏购买平台,不同于亚马逊购买DVD光盘,它支持从游戏库购买数字发行版体验游戏.另外,它也不同于 ...

  5. 如何解决python中使用flask时遇到的markupsafe._compat包缺失的问题

    在使用python进行GUI的程序编写时,使用flask时出现错误: 在使用pip freeze进行查看已下载的包时显示MarkupSafe与Jinjia2都已安装: 在网上查阅一些资料后发现,在py ...

  6. 201621123031 《Java程序设计》第14周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计. 答 ...

  7. 北亚关于HP EVA4400/6400/8400/P6000的数据恢复解决方案

    [引言]本文档建立在针对HP EVA的大量测试性研究基础上,所有的细节几乎均为对EVA的破译型研究,目前全球范围内尚未发现类似资料,故可能表述方式和结论并不精确,仅为参考之用.我们公司为研究HP EV ...

  8. C# 封装miniblink 使用HTML/CSS/JS来构建.Net 应用程序界面和简易浏览器

    MiniBlink的作者是 龙泉寺扫地僧 miniblink是什么?   (抄了一下 龙泉寺扫地僧 写的简洁) Miniblink是一个全新的.追求极致小巧的浏览器内核项目,其基于chromium最新 ...

  9. python django的ManyToMany简述

    Django的多对多关系 在Django的关系中,有一对一,一对多,多对多的关系 我们这里谈的是多对多的关系 ==我们首先来设计一个用于示例的表结构== # -*- coding: utf-8 -*- ...

  10. LeetCode & Q27-Remove Element-Easy

    Array Two Pointers Description: Given an array and a value, remove all instances of that value in pl ...