LeetCode 533. Lonely Pixel II (孤独的像素之二) $
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:
- Row R and column C both contain exactly N black pixels.
- 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:
- The range of width and height of the input 2D array is [1,200].
题目标签:Array
题目给了我们一个2D array picture 和一个N,让我们从picture 中找到 符合 两条规则的black pixel的个数。
rule 2 真是一开始没理解,琢磨了一会,去看了解释才明白。
rule 2 说的是,当满足了rule 1 有一个在row R 和 column C 的black pixel,这个像素的行 和列 都要有N个 black pixels之后,
还需要满足column C 中 有black pixels 的 rows 都要和 R 这一行row 一摸一样。
建立一个Map,把row String(把每一行char组成string) 当作key, 把这一个row 出现过的次数 当作value;还需要建立一个cols array,来记录每一列的black pixels个数。
遍历picture:
1. 记录每一列的B 个数;
2. 记录每一行的B 个数,只有等于N的情况下,才把row string 存入map。(每一行的black pixels个数在这里就被完成了,所以不需要额外空间来存放)
遍历map 的keySet(有N个B的行):
1. 如果这个row出现的次数 不等于 N的话, 说明 不满足rule 1的一列里要有N个B的条件。 因为如果当前 row 出现了N次,而且row 在之前已经满足了一行里有N个B的条件。所以每行里肯定有B,然后有B的一列里也会有N个B。
当满足了row出现的次数 等于N 之后,意味着也满足了rule 2,因为这N个rows 都是一摸一样的。
2. 当满足了上面这个条件后,遍历所有列:把每列的B的个数N加入res。
Java Solution:
Runtime beats 71.04%
完成日期:09/25/2017
关键词:Array, HashMap
关键点:建立一个HashMap,使得每一行的string 和 它的出现次数形成映射(满足rule1 rule2);建立array cols 来辅助找到每一列中符合标准的B的数量
class Solution
{
public int findBlackPixel(char[][] picture, int N)
{
int m = picture.length;
int n = picture[0].length;
int[] cols = new int[n];
HashMap<String, Integer> map = new HashMap<>();
int res = 0; // iterate picture
for(int i=0; i<m; i++) // rows
{
int count = 0;
StringBuilder sb = new StringBuilder(); for(int j=0; j<n; j++) // cols
{
if(picture[i][j] == 'B')
{
cols[j]++;
count++;
}
sb.append(picture[i][j]);
} if(count == N) // only store rowString into map when this row has N B
{
String curRow = sb.toString();
map.put(curRow, map.getOrDefault(curRow, 0) + 1); // count how many rows are same
} } // iterate keySet
for(String row : map.keySet())
{
if(map.get(row) != N) // if value is not N, meaning rule 1 (columns need to have N Bs) is not satisfied.
continue;
// if value is N, meaning rule 2 is also satisfied because all these rows are same.
for(int j=0; j<n; j++) // iterate column
{ // count Bs in this column
if(row.charAt(j) == 'B' && cols[j] == N)
res += N;
}
} return res;
}
}
参考资料:
https://discuss.leetcode.com/topic/81686/verbose-java-o-m-n-solution-hashmap
LeetCode 题目列表 - LeetCode Questions List
LeetCode 533. Lonely Pixel II (孤独的像素之二) $的更多相关文章
- [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 ...
- [LeetCode] Lonely Pixel II 孤独的像素之二
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- [LeetCode] 531. Lonely Pixel I 孤独的像素 I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- [LeetCode] Lonely Pixel I 孤独的像素之一
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- 533. Lonely Pixel II
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- LeetCode 531. Lonely Pixel I
原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] 685. Redundant Connection II 冗余的连接之二
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...
- [LeetCode] Shortest Word Distance II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
随机推荐
- Java: private、protected、public和default的区别
public: 具有最大的访问权限,可以访问任何一个在classpath下的类.接口.异常等.它往往用于对外的情况,也就是对象或类对外的一种接口的形式. protected: 主要的作用就是用来保护子 ...
- Java: AutoCloseable接口
K7 增加了一些新特性,其中报错AutoCloseable 等.新特性包括如下,下面主要说说AutoCloseable . 在JDK7 中只要实现了AutoCloseable 或Closeable 接 ...
- SAP高可用性(HA)
1.SAP系统高可用的要求 高可用性是从终端用户的角度来需求,及要求最大化系统的可用性.其目的是降低意外系统关闭时间(服务器生效.存储失效.操作系统失败--),减少预期系统关闭时间(系统及架构的维护. ...
- SpringMVC第七篇【RESTful支持、拦截器】
RESTful支持 我们在学习webservice的时候可能就听过RESTful这么一个名词,当时候与SOAP进行对比的-那么RESTful究竟是什么东东呢??? RESTful(Representa ...
- oracle 表查询(一)
通过scott用户下的表来演示如何使用select语句,接下来对emp.dept.salgrade表结构进行解说. emp 雇员表字段名称 数据类型 是否为空 备注-------- ...
- [js高手之路] html5 canvas系列教程 - arcTo(弧度与二次,三次贝塞尔曲线以及在线工具)
之前,我写了一个arc函数的用法:[js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形). arcTo: cxt.arcTo( cx, cy, x2, y2, ...
- python django 使用 haystack:全文检索的框架
haystack:全文检索的框架whoosh:纯Python编写的全文搜索引擎jieba:一款免费的中文分词包 首先安装这三个包 pip install django-haystackpip inst ...
- 安装myeclipse2015 stable 3.0破解之后发生出现SECURITY ALERT:iNTEGRITY CHECK ERROR然后闪退解决方案
安装好myeclipse2015 stable以后也一步步按着破解文件的步骤来进行.打开myEclipse---->Subscription information--->Subscrip ...
- 在JavaScript中使用json.js:Ajax项目之POST请求(异步)
经常在百度搜索框输入一部分关键词后,弹出候选关键热词.现在我们就用Ajax技术来实现这一功能. 一.下载json.js文件 百度搜一下,最好到json官网下载,安全起见. 并与新建的两个文件部署如图 ...
- Cornfields poj2019 二维RMQ
Cornfields Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Submit S ...