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通过链表实现队列
class LinkedQueue<T> { /** * 队列大小,由构造函数初始化 */ private int maxSize; /** * 队头 */ private Node fr ...
- 深入浅出数据结构C语言版(19)——堆排序
在介绍优先队列的博文中,我们提到了数据结构二叉堆,并且说明了二叉堆的一个特殊用途--排序,同时给出了其时间复杂度O(N*logN).这个时间界是目前我们看到最好的(使用Sedgewick序列的希尔排序 ...
- Java并发之线程间的协作
上篇文章我们介绍了synchronized关键字,使用它可以有效的解决我们多线程所带来的一些常见问题.例如:竞态条件,内存可见性等.并且,我们也说明了该关键字主要是一个加锁和释放锁的集成,所有为能获得 ...
- Android 之内容提供者 内容解析者 内容观察者
contentProvider:ContentProvider在Android中的作用是对外提供数据,除了可以为所在应用提供数据外,还可以共享数据给其他应用,这是Android中解决应用之间数据共享的 ...
- 【SQL】- 基础知识梳理(六) - 游标
游标的概念 结果集,结果集就是select查询之后返回的所有行数据的集合. 游标(Cursor): 是处理数据的一种方法. 它可以定位到结果集中的某一行,对数据进行读写. 也可以移动游标定位到你需要的 ...
- 一款简洁而强大的前端框架JQUery—动画效果及剪刀石头布小游戏
jQuery是什么? jQuery是一个快速.简洁的JavaScript框架,它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作.事件处理.动画 ...
- Canvas+Video打造酷炫播放体验
一.简介 直到现在,仍然不存在一项旨在网页上显示视频的标准. 今天,大多数视频是通过插件(比如 Flash)来显示的.然而,并非所有浏览器都拥有同样的插件. HTML5 规定了一种通过 video 元 ...
- oracle 数据库(表)的逻辑备份与恢复
一.介绍逻辑备份是指使用工具export将数据对象的结构和数据导出到文件的过程.逻辑恢复是指当数据库对象被误操作而损坏后使用工具import利用备份的文件把数据对象导入到数据库的过程.物理备份即可在数 ...
- 【TOMCAT启动异常】The BASEDIR environment variable is not defined correctly
<span style="font-size:18px;">The BASEDIR environment variable is not defined correc ...
- U盘中毒无限蓝屏重启的解决办法
开门见山,这个帖子只针对U盘中毒导致的以下两种症状: 1.win10系统无法进入并且要求初始化,卸载所有第三方应用 2.win7系统无限蓝屏重启): 其他的硬件故障不在本次讨论范围之内. 说明以下.上 ...