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 Rand 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].
本题读了好几遍题目也没有怎么读懂,有点小难了,那两个限制条件的大致意思是,第一,某一点为B的点,它的行和列的B的个数都是N,第二个意思是,每一行里面出现的B,B的整个列为B的行必须和该B的行的字符顺序是一样的,代码如下:
public class Solution {
public int findBlackPixel(char[][] picture, int N) {
int row = picture.length;
int col = picture[0].length;
int[] colcount =new int[col];
Map<String,Integer> map = new HashMap<>();
for(int i=0;i<row;i++){
String s = scanRow(picture,N,colcount,i);
if(s.length()!=0)
map.put(s,map.getOrDefault(s,0)+1);
}
int res = 0;
for(String key:map.keySet()){
if(map.get(key)==N){
for(int i=0;i<col;i++){
if(key.charAt(i)=='B'&&colcount[i]==N){
res+=N;
}
}
}
}
return res; }
public String scanRow(char[][] picture,int N,int[] colcount,int row){
StringBuilder sb = new StringBuilder();
int col = picture[0].length;
int count = 0;
for(int i=0;i<col;i++){
if(picture[row][i]=='B'){
count++;
colcount[i]++;
}
sb.append(picture[row][i]);
}
if(count==N) return sb.toString();
else return "";
}
}
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 533. Lonely Pixel 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 ...
- 531. Lonely Pixel I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- LeetCode 531. Lonely Pixel I
原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...
- LeetCode 533----Lonely Pixel II
问题描述 Given a picture consisting of black and white pixels, and a positive integer N, find the number ...
- LeetCode Questions List (LeetCode 问题列表)- Java Solutions
因为在开始写这个博客之前,已经刷了100题了,所以现在还是有很多题目没有加进来,为了方便查找哪些没加进来,先列一个表可以比较清楚的查看,也方便给大家查找.如果有哪些题目的链接有错误,请大家留言和谅解, ...
随机推荐
- eltwise层
http://blog.csdn.net/u013989576/article/details/73294131 layer { name: "fuse" type: " ...
- [BZOJ2938]病毒 (AC自动机+dfs)
题目描述 二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码.如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的.现在委员会已经找出了所有的病毒代码段,试问,是否 ...
- Go统计键盘输入随机字母的个数
package main import "fmt" //通过键盘输入20个小写字母,统计个数 func main(){ //通过键盘器获取字符 var arr [20]byte f ...
- python--MySQL权限管理 数据备份还原
一 权限管理 mysql最高管理者是root用户, 这个一般掌握在公司DBA手里, 当你想去对数据库进行一些操作的时候,需要DBA授权给你. 1. 对新用户增删改 1. 创建用户 # 要先use my ...
- Django ORM字段和字段参数
Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...
- django第六天(模板相关,过滤器和标记)
django第6天 DTL简介 django template language django模板语言 语法: filter{{}} tag{% %} 简单变量的使用 视图函数可以通过两种方式将变量船 ...
- Beyond Compare 30天评估期结束解决办法
打开Beyond Compare 4,提示已经超出30天试用期限制 解决方法: 1.修改文件 修改C:\Program Files\Beyond Compare 4\BCUnrar.dll ,这个文件 ...
- 小白用shiro(2)
本文来自网易云社区 作者:王飞 以上的配置走完以后就可以用,下面讲讲个人需求,以及踩过的坑: 1.如何修改cookie的名称,默认名称"rememberMe"太丑了有木有? 首先丢 ...
- centos 7 smplayer vlc播放器
centos7安装多媒体播放器SMPlayer 2017-03-13 21:37:14 分类: LINUX 转自:https://wiki.centos.org/TipsAndTricks/Multi ...
- [android开发篇]activity组件篇
https://developer.android.com/guide/components/activities.html Activity 是一个应用组件,用户可与其提供的屏幕进行交互,以执行拨打 ...